Bash Commands Note
Posted on: August 15, 2023
Note: filename/foldername/destination can be a path to that file/folder
echo
- To print something in terminal:
echo <text>
echo hello world// terminal prints "hello world"
- To print something to a file instead of terminal:
echo <text> >> <filename>
echo hello world >> README.md// terminal will not print "hello world", but it is shown in README.md
-
Get manual / help menu:
man <command>
orhelp echo
,echo --help
does not work -
Enable backslash escapes:
echo -e <backslash_command>
echo -e "\n<text>" // only prints empty lines if the value is enclosed in quotes
pwd - print working directory
- To see the full path of the current folder
ls - list
-
To see what is inside the current folder
-
To list contents in a "long list format":
ls -l
-
To display "help" menu for
ls
command:ls --help
-
To list all contents:
ls --all
orls -a
-
To list a specified folder:
ls <path_to_folder>
cd - change directory
-
To go into a folder:
cd <folder_name>
-
To go back a folder level:
cd ..
-
To go back two folder levels:
cd ../..
, each set of dots represent a folder level -
To go back the folder you are in:
cd .
more
- To see what is inside a file:
more <filename>
cat
- To print the contents of a file:
cat <filename>
clear
- To clear terminal
mkdir - make directory
- To make a new folder:
mkdir <folder_name>
ormkdir <existing_folder_name>/<new_folder_name>
touch
- To make a new file:
touch <filename>
cp - copy
To copy a file into a folder: cp <file> <destination>
To make a copy of the file: cp <file> <new_filename>
-
To get "help" menu of
cp
command:cp --help
-
To copy the whole folder:
cp -r <file> <destination>
rm - remove
-
To remove a file:
rm <filename>
-
To get "help" menu of
rm
command:rm --help
rmdir - remove directory
-
To remove an empty folder:
rmdir <directory_name>
-
To remove a non-empty folder and everything inside it:
rm -r <foldername>
mv - move
-
To rename a file:
mv <filename> <new_filename>
-
To move a file:
mv <file> <destination>
find
-
To view a file tree (i.e see all files and folders) of the current folder. Unlike
ls
,find
can view all the files inside the folders in this current folder (i.e many folder levels down), not just only the files in the current folder level. -
To view a file tree of a different folder:
find <folder_name>
-
To get "help" menu of
find
command:find --help
-
To find the path of a file or a folder:
find -name <filename_or_foldername>
pg_dump
- To dump a database as a text file or to other formats
pg_dump --clean --create --inserts --username=freecodecamp students > students.sql// This dumps the 'students' database into a 'students.sql' file saving all the commands needed to rebuild the database.
-
To get help menu:
pg_dump --help
-
To rebuild the database:
psql -U postgres < <sql_filename>
exit
- To exit the terminal