Linux and Bash Command Cheat Sheet: The Basics

prajwal056

Prajwal k

Posted on June 26, 2022

Linux and Bash Command Cheat Sheet: The Basics

Getting information

Return your user name

whoami
Enter fullscreen mode Exit fullscreen mode

Return your user and group id

id
Enter fullscreen mode Exit fullscreen mode

Return operating system name, username, and other info

uname -a
Enter fullscreen mode Exit fullscreen mode

Display reference manual for a command

man top
Enter fullscreen mode Exit fullscreen mode

Get help on a command

curl --help
Enter fullscreen mode Exit fullscreen mode

Return the current date and time

date
Enter fullscreen mode Exit fullscreen mode

Monitoring performance and status

List selection of or all running processes and their PIDs

ps
ps -e
Enter fullscreen mode Exit fullscreen mode

Display resource usage

top
Enter fullscreen mode Exit fullscreen mode

List mounted file systems and usage

df
Enter fullscreen mode Exit fullscreen mode

Working with files

Copy a file

cp file.txt new_path/new_name.txt
Enter fullscreen mode Exit fullscreen mode

Change file name or path

mv this_file.txt that_path/that_file.txt
Enter fullscreen mode Exit fullscreen mode

Remove a file verbosely

rm this_old_file.txt -v
Enter fullscreen mode Exit fullscreen mode

Create an empty file, or update existing file's timestamp

touch a_new_file.txt
Enter fullscreen mode Exit fullscreen mode

Change/modify file permissions to 'execute' for all users

chmod +x my_script.sh
Enter fullscreen mode Exit fullscreen mode

Get count of lines, words, or characters in file

wc -l table_of_data.csv
wc -w my_essay.txt
wc -m some_document.txt
Enter fullscreen mode Exit fullscreen mode

Return lines matching a pattern from files matching a filename pattern - case insensitive and whole words only

grep -iw hello \*.txt
Enter fullscreen mode Exit fullscreen mode

Return file names with lines matching the pattern 'hello' from files matching a filename pattern

grep -l hello \*.txt
Enter fullscreen mode Exit fullscreen mode

Navigating and working with directories

List files and directories by date, newest last

ls -lrt
Enter fullscreen mode Exit fullscreen mode

Find files in directory tree with suffix 'sh'

find -name '\*.sh'
Enter fullscreen mode Exit fullscreen mode

Return present working directory

pwd
Enter fullscreen mode Exit fullscreen mode

Make a new directory

mkdir new_folder
Enter fullscreen mode Exit fullscreen mode

Change the current directory: up one level, home, or some other path

cd ../
cd ~ or cd
cd another_directory
Enter fullscreen mode Exit fullscreen mode

remove directory, verbosely

Printing file and string contents

Print file contents

cat my_shell_script.sh
Enter fullscreen mode Exit fullscreen mode

Print file contents page-by-page

more ReadMe.txt
Enter fullscreen mode Exit fullscreen mode

Print first N lines of file

head -10 data_table.csv
Enter fullscreen mode Exit fullscreen mode

Print last N lines of file

tail -10 data_table.csv
Enter fullscreen mode Exit fullscreen mode

Print string or variable value

echo "I am not a robot"
echo "I am $USERNAME"
Enter fullscreen mode Exit fullscreen mode

Compression and archiving

Archive a set of files

tar -cvf my_archive.tar.gz file1 file2 file3
Enter fullscreen mode Exit fullscreen mode

Compress a set of files

zip my_zipped_files.zip file1 file2
zip my_zipped_folders.zip directory1 directory2
Enter fullscreen mode Exit fullscreen mode

Extract files from a compressed zip archive

unzip my_zipped_file.zip
unzip my_zipped_file.zip -d extract_to_this_direcory
Enter fullscreen mode Exit fullscreen mode

Performing network operations

Print hostname

hostname
Enter fullscreen mode Exit fullscreen mode

Send packets to URL and print response

ping www.google.com
Enter fullscreen mode Exit fullscreen mode

Display or configure system network interfaces

ifconfig
ip
Enter fullscreen mode Exit fullscreen mode

Display contents of file at a URL

curl <url>
Enter fullscreen mode Exit fullscreen mode

Download file from a URL

wget <url>
Enter fullscreen mode Exit fullscreen mode

Bash shebang

!/bin/bash

Enter fullscreen mode Exit fullscreen mode

Pipes and Filters

Chain filter commands using the pipe operator

ls | sort -r
Enter fullscreen mode Exit fullscreen mode

Pipe the output of manual page for ls to head to display the first 20 lines

man ls | head -20
Enter fullscreen mode Exit fullscreen mode

Shell and Environment Variables

List all shell variables

set
Enter fullscreen mode Exit fullscreen mode

Define a shell variable called my_planet and assign value Earth to it

my_planet=Earth
Enter fullscreen mode Exit fullscreen mode

Display shell variable

echo $my_planet
Enter fullscreen mode Exit fullscreen mode

List all environment variables

env
Enter fullscreen mode Exit fullscreen mode

Environment vars: define/extend variable scope to child processes

export my_planet
export my_galaxy='Milky Way'
Enter fullscreen mode Exit fullscreen mode

Metacharacters

Comments

# The shell will not respond to this message
Enter fullscreen mode Exit fullscreen mode

Command separator

echo 'here are some files and folders'; ls
Enter fullscreen mode Exit fullscreen mode

File name expansion wildcard

ls *.json
Enter fullscreen mode Exit fullscreen mode

Single character wildcard

ls file_2021-06-??.json
Enter fullscreen mode Exit fullscreen mode

Quoting

Single quotes - interpret literally

echo 'My home directory can be accessed by entering: echo $HOME'
Enter fullscreen mode Exit fullscreen mode

Double quotes - interpret literally, but evaluate metacharacters

echo "My home directory is $HOME"
Enter fullscreen mode Exit fullscreen mode

Backslash - escape metacharacter interpretation

echo "This dollar sign should render: \$"
Enter fullscreen mode Exit fullscreen mode

I/O Redirection

Redirect output to file

echo 'Write this text to file x' > x
Enter fullscreen mode Exit fullscreen mode

Append output to file

echo 'Add this line to file x' >> x
Enter fullscreen mode Exit fullscreen mode

Redirect standard error to file

bad_command_1 2> error.log
Enter fullscreen mode Exit fullscreen mode

Append standard error to file

bad_command_2 2>> error.log
Enter fullscreen mode Exit fullscreen mode

Redirect file contents to standard input

$ tr “[a-z]” “[A-Z]” < a_text_file.txt
Enter fullscreen mode Exit fullscreen mode

The input redirection above is equivalent to

$cat a_text_file.txt | tr “[a-z]” “[A-Z]”
Enter fullscreen mode Exit fullscreen mode

Command Substitution

Capture output of a command and echo its value

THE_PRESENT=$(date)
echo "There is no time like $THE_PRESENT"
Enter fullscreen mode Exit fullscreen mode

Command line arguments

./My_Bash_Script.sh arg1 arg2 arg3
Enter fullscreen mode Exit fullscreen mode

Batch vs. concurrent modes
Run commands sequentially

start=$(date); ./MyBigScript.sh ; end=$(date)
Enter fullscreen mode Exit fullscreen mode

Run commands in parallel

./ETL_chunk_one_on_these_nodes.sh & ./ETL_chunk_two_on_those_nodes.sh
Enter fullscreen mode Exit fullscreen mode

Scheduling jobs with Cron

Open crontab editor

crontab -e
Enter fullscreen mode Exit fullscreen mode

Job scheduling syntax

m h dom mon dow command
Enter fullscreen mode Exit fullscreen mode

minute, hour, day of month, month, day of week
'*' means any

Append the date/time to file every Sunday at 6:15 pm

15 18 * * 0 date >> sundays.txt
Enter fullscreen mode Exit fullscreen mode

Run a shell script on the first minute of the first day of each month

1 0 1 * * ./My_Shell_Script.sh
Enter fullscreen mode Exit fullscreen mode

Back up your home directory every Monday at 3 am

0 3 * * 1 tar -cvf my_backup_path\my_archive.tar.gz $HOME\
Enter fullscreen mode Exit fullscreen mode

Deploy your cron job

Close the crontab editor and save the file
Enter fullscreen mode Exit fullscreen mode

List all cron jobs

crontab -l
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
prajwal056
Prajwal k

Posted on June 26, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related