Make directory
mkdir folder_name
Can create multiple directories in one command
mkdir Jan Feb Mar
Change directory
cd folder_name
Home directory: cd ~
or cd
Use cases:
- Create a New File if it doesn't exist
- If the file exists it updates its timestamp
touch file_name
touch file1 file2 file3
Copy file
cp ~/.bashrc bashrc
cp bashrc bashrc.bak
Use cases:
- move a file
- rename a file
mv bashrc.bk bashrc
Delete files. Be careful, as there is no way to restore the files.
rm file1
rm file*
- to remove directory use r option (recursive):
rm -r dir1
Delete (empty) directories
rmdir *
will delete all empty directories in the current path
clear
clears the screen.
Shortcut: ctrl+l
ls
List all files (including hidden files)
ls -a
Use cases:
- print a text file
cat readme.md
- create a text file
cat >> file1.txt
- start typing and when you are done, press
ctrl+d
. The file is created and stored. - concatenate multiple files
cat file1 file2
Edit a file
nano file_name
To save:
- control + o
- enter
- control + x
echo "text here" >> filename
Examples:
pip list | grep conda
cat file_name | grep keyword
Add \ before space.
cat my\ script.py
export MY_ENV=125
echo $MY_ENV
echo $PATH
head data.csv
head -n 5 data.csv
tail data.csv
tail -n 5 dada.csv
cat data.csv | more
cat data.csv | less
Displays the contents of a file on the screen with a few functionalities (search, next and previous page). When starting less doesn’t read the entire file which results in much faster load times compared to text editors like vim or nano.
less filename
Options:
- Spacebar : To go to the next screen
- b: to go to the previous screen
- /: to search for a specific word
- q: quit
If you want less to shows line numbers launch the program with the -N option:
less -N filename
The +F option tells less to watch the file contents for changes. This is useful when opening log files (behaves pretty much the same as tail -f
).
less +F /var/log/messages
Show (the head of) the second column of a comma separated file
cat gdp.csv | head | cut -d "," -f 2
Show unique values of column 2:
cat gdp.csv | cut -d "," -f 2 | sort | uniq -c
wc data.csv
a one line description of the command
whatis cal
Don't know the name of a command
apropos time
sort --help
Display man (user manual)
man bash
man ls
find a file
locate bash
to update the locate database for search:
sudo updatedb
Finds if a command exists and where it exists when you run it
which python
shows the history of commands
history
- https://medium.com/data-science-bootcamp/command-line-cheat-sheet-for-data-scientists-397e63c4ba5f
- https://towardsdatascience.com/using-bash-for-data-pipelines-cf05af6ded6f
- https://www.youtube.com/watch?v=L2XJ7WJTPbA
- https://towardsdatascience.com/basics-of-bash-for-beginners-92e53a4c117a
- http://omgenomics.com/bash-intro/