Bash commands for beginners
Bash is the defacto shell for Linux and Unix-like os. Getting familiar with the command line is super important as you will regularly find yourself traversing files from the command line. While I am no expert, I have been dedicating myself to learning the command line better so I can be more efficient in my job as a dev and avoid needless keystrokes and clicks to access a file.
Here are 10 commands that I find myself using often that can help you start your bash journey, along with some cool options to run with them.
#1 — ls
ls
lists all of the files or directories within the current directory.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUTlsOUTPUTbirds populations.txt
ls -l
shows file or directory, size, modified date and time, file or folder name, and owner of the file and its permission.
drwxr-xr-x@ 5 manthony staff 160 Feb 1 16:51 birds
-rw-r — r — @ 1 manthony staff 0 Feb 1 16:50 populations.txt
ls -a
List all files including hidden files starting with ‘.‘.
. .. birds populations.txt
#2 — pwd
pwd
stands for print working directory. It will do just that- print the file path of the current directory that you are in inside of the terminal.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUTpwdOUTPUT/Users/manthony/Desktop/animals
#3 — cd
cd
stands for change directory. Use it to advance to a new directory either higher or lower in the file tree.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUTcd birds
lsOUTPUTcardinal.txt nonflying pidgeon.txt/animals
├── populations.txt
├── birds <-- current dir
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txt
#4 — touch
touch
will create a new file in the target directory.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUTtouch endangered_species.txt
lsOUTPUTbirds endangered_species.txt populations.txt/animals <-- current dir
├── populations.txt
├── endangered_species.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txt
#5 — mkdir
Similar to touch
, mkdir
will create a directory.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUTmkdir reptiles
lsOUTPUTbirds populations.txt reptiles/animals <-- current dir
├── populations.txt
├── reptiles
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txt
#6 — rm
rm
will delete the target file. Use rm -r
to delete a directory and all files within it.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── endangered_species.txt
├── reptiles
│ ├── lizard.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUTrm endangered_species.txt
rm -r reptiles
lsOUTPUTbirds populations.txt/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txt
#7 — cat
cat
will print the content of the specified file.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUTcat populations.txtOUTPUTSome great info about populations
...and even more info!
cat -n
will operate the same as cat
and include line numbers making it more readable.
1 Some great info about populations
2 ...and even more info!
#8 — mv
Use mv
to move files or directories.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUTmv hawk.txt birds
lsOUTPUTbirds populations.txt/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── hawk.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txt
#8 — cp
Use cp
to copy files or directories.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUTcp populations.txt birds
lsOUTPUTbirds populations.txt/animals <-- current dir
├── populations.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── populations.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txt
#9 — < and >
Most of the commands above serve as standard output commands- that is they print their output to the terminal. By using >
or <
you are executing the command as a redirected output, meaning that it can output to other files or take input from other files. In this case, these commands will overwrite the file with the output.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── populations_additional.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUTcat populations.txt > populations_additional.txt
cat populations.txt
cat populations_additional.txtOUTPUT//populations.txt
Some great info about populations
...and even more info!//populations_additional.txt
Some great info about populations
...and even more info!
#10 — << and >>
Very similar looking to the <
and >
commands, <<
and >>
will not overwrite but rather append output to a file.
FILE STRUCTURE/animals <-- current dir
├── populations.txt
├── populations_additional.txt
├── birds
│ ├── cardinal.txt
│ ├── pidgeon.txt
│ ├── nonflying
│ │ ├── chicken.txt
│ │ ├── turkey.txtINPUT//populations.txt
Some great info about populations
...and even more info!//populations_additional.txt
What a great featurecat populations_additional.txt >> populations.txt
cat populations.txt
cat populations_additional.txtOUTPUT//populations.txt
Some great info about populations
...and even more info!
What a great feature//populations_additional.txt
What a great feature
I hope that this was a helpful and gentle introduction to some really useful commands.
A dog-loving full stack developer and Marine veteran from New Haven, CT that thoroughly enjoys reading and sharing to solidify his own learning.