Skip to content

Instantly share code, notes, and snippets.

@ryanbehdad
Last active July 10, 2020 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbehdad/e76fb919150333a3715e4a073506ba65 to your computer and use it in GitHub Desktop.
Save ryanbehdad/e76fb919150333a3715e4a073506ba65 to your computer and use it in GitHub Desktop.
bash commands

bash commands

mkdir

Make directory mkdir folder_name

Can create multiple directories in one command

mkdir Jan Feb Mar

cd

Change directory

cd folder_name

Home directory: cd ~ or cd

touch

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

cp

Copy file

cp ~/.bashrc bashrc

cp bashrc bashrc.bak

mv

Use cases:

  • move a file
  • rename a file mv bashrc.bk bashrc

rm

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

rmdir

Delete (empty) directories rmdir * will delete all empty directories in the current path

clear

clear clears the screen.

Shortcut: ctrl+l

List Files

ls

List all files (including hidden files) ls -a

cat

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

nano

Edit a file nano file_name

To save:

  • control + o
  • enter
  • control + x

Appending text to a file

echo "text here" >> filename

Pipe | grep

Examples:

  • pip list | grep conda
  • cat file_name | grep keyword

Handling a filename with a space

Add \ before space. cat my\ script.py

Environment variables

export MY_ENV=125

echo $MY_ENV

echo $PATH

Head

head data.csv

head -n 5 data.csv

Tail

tail data.csv

tail -n 5 dada.csv

print file page by page

cat data.csv | more

cat data.csv | less

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

cut

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

word count

wc data.csv

Help

whatis

a one line description of the command whatis cal

apropos

Don't know the name of a command

apropos time

help

sort --help

man

Display man (user manual)

man bash

man ls

locate

find a file locate bash

to update the locate database for search: sudo updatedb

which

Finds if a command exists and where it exists when you run it

which python

history

shows the history of commands

history

Sources

  1. https://medium.com/data-science-bootcamp/command-line-cheat-sheet-for-data-scientists-397e63c4ba5f
  2. https://towardsdatascience.com/using-bash-for-data-pipelines-cf05af6ded6f
  3. https://www.youtube.com/watch?v=L2XJ7WJTPbA
  4. https://towardsdatascience.com/basics-of-bash-for-beginners-92e53a4c117a
  5. http://omgenomics.com/bash-intro/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment