Bash commands for beginners - 01/02/2023
Basic bash commands for everyday use
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.txt``````INPUT``````ls``````OUTPUT``````birds populations.txt
2 **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.txt``````INPUT``````pwd``````OUTPUT``````/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.txt``````INPUT``````cd birdsls``````OUTPUT``````cardinal.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.txt``````INPUT``````touch endangered_species.txtls``````OUTPUT``````birds endangered_species.txt populations.txt``````/animals <-- current dir├── populations.txt├── endangered_species.txt├── birds│ ├── cardinal.txt│ ├── pidgeon.txt│ ├── nonflying│ │ ├── chicken.txt│ │ ├── turkey.txt
---##
5 - mkdirSimilar to touch
, mkdir
will create a directory.FILE STRUCTURE``````/animals <-- current dir├── populations.txt├── birds│ ├── cardinal.txt│ ├── pidgeon.txt│ ├── nonflying│ │ ├── chicken.txt│ │ ├── turkey.txt``````INPUT``````mkdir reptilesls``````OUTPUT``````birds 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.txt``````INPUT``````rm endangered_species.txtrm -r reptilesls``````OUTPUT``````birds 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.txt``````INPUT``````cat populations.txt``````OUTPUT``````Some 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 populations2 ...and even more info!
---##
8 - mvUse mv
to move files or directories.FILE STRUCTURE``````/animals <-- current dir├── populations.txt├── birds│ ├── cardinal.txt│ ├── pidgeon.txt│ ├── nonflying│ │ ├── chicken.txt│ │ ├── turkey.txt``````INPUT``````mv hawk.txt birdsls``````OUTPUT``````birds populations.txt``````/animals <-- current dir├── populations.txt├── birds│ ├── cardinal.txt│ ├── pidgeon.txt│ ├── hawk.txt│ ├── nonflying│ │ ├── chicken.txt│ │ ├── turkey.txt
---## #8 - cpUse **cp
**to copy files or directories.FILE STRUCTURE``````/animals <-- current dir├── populations.txt├── birds│ ├── cardinal.txt│ ├── pidgeon.txt│ ├── nonflying│ │ ├── chicken.txt│ │ ├── turkey.txt``````INPUT``````cp populations.txt birdsls``````OUTPUT``````birds 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.txt``````INPUT``````cat populations.txt > populations_additional.txtcat populations.txtcat populations_additional.txt``````OUTPUT``````//populations.txtSome great info about populations...and even more info!``````//populations_additional.txtSome 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.txt``````INPUT``````//populations.txtSome great info about populations...and even more info!``````//populations_additional.txtWhat a great feature``````cat populations_additional.txt >> populations.txtcat populations.txtcat populations_additional.txt``````OUTPUT``````//populations.txtSome great info about populations...and even more info!What a great feature``````//populations_additional.txtWhat 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.__https://grandorimichael.medium.com/