Basic Linux Commands and Tools

Sudarshan Pathak
7 min readAug 15, 2023

--

Generated using Leonardo.AI

Linux is the kernel of free and open source operating system which was developed by Linux Torvalds. To know more about Linux Shell and Commands refer to my previous article Introduction to Linux Shell and Commands.

Why learn Linux?

Linux is widely used in hosting and server infrastructure. As most of the software developed by developers end up running in Linux servers, learning Linux helps developers increase their efficiency and develop scalable and stable software. Learning linux contribute to overall growth and effectiveness of developers in their professional endeavors.

Basic Linux Commands

In this article we will focus on the basic and essential commands in linux that are required to do any tasks in linux. In this article the commands are structured in a specific way. The heading contains command followed by command’s short description. Command heading is followed by command description (taken from man page of the command). Below the command description are command usage examples with description in comments.

ls — list directory contents

List information about the FILEs (the current directory by default).

ls          #list current directory contents
ls /etc/ #list /etc/ directory
ls -l #list files in long format
ls -lh #list files in long format and show file size in human readable format (KB, MB, GB, etc)
ls -a #show all files (include hidden files)
ls -ltrha #sort by time (r to reverse order)
ls -lS #sort by size (include -r to reverse order)
ls -m /etc/ #list files separated by comma
ls -G #enable colorized output (can use in combination ex. ls -lhaG)
ls -d ./* #list only directories
ls -R #recursiverly list files (all files within all subdirectories down to the last file)

ls ~/web/*.php #list only php files in web directory inside home directory
ls -la ~/web/*user*.php #list php files with user in filename

ls --help #view all ls options (man ls)

cd — change the working directory

The cd utility shall change the working directory of the current shell execution environment

cd /home/ubuntu
cd ~/Desktop

pwd — return working directory name

The pwd utility writes the absolute pathname of the current working directory to the standard output.

pwd #returns current working directory path

echo — write arguments to the standard output

The echo utility writes any specified operands followed by a newline (‘\n’) character, to the standard output.

echo "This will be displayed as it is"
echo "$HISTSIZE of command history will be saved" #$HISTSIZE will be replaced by it's value
echo '$HISTSIZE of command history will be saved' #this will be displayed as it is
echo "Server `hostname`, Logged in user is `whoami` and date is `date`" #command within `` will be replaced by their output

mkdir — make directories

The mkdir utility creates the directories named as operands, in the order specified, using mode rwxrwxrwx (777) as modified by the current umask.

mkdir dir1
mkdir -p dir1/new-dir/nested #Create intermediate directories as required.
mkdir -pv dir2/new/test #create and list newely created directories
mkdir dir3 dir4 dir5 dir6 #create multiple directories (shorthand mkdir dir{1,2,3,4})

touch — change file access and modification times

The touch utility sets the modification and access times of files. If any file does not exist, it is created with default permissions. Use stat command to view file date and time information.

touch newfile.txt          #create new file
touch notes.txt #update access time and modification times to current time
touch -a notes.txt #only update access time
touch -m notes.txt #only update modification time
touch -r log.txt notes.txt #use log.txt time to update time of notes.txt
touch 01101455 notes.txt #change file time to specific date and time
touch -c error_log #Do not create the file if it does not exist

stat— display file status

The stat utility displays information about the file.

stat -x notes.txt #show file information in verbose form
stat -s notes.txt #display information in “shell output” format,

cat — concatenate and print files

The cat utility reads files sequentially, writing them to the standard output.

cat              #read from stdin (standard input) and write to stdout (standard output)
cat notes.txt #read file and write to stdout
cat -n notes.txt #show line numbers
cat > newfile #create new file
cat *.txt #read all txt files in current directory

cat notes.txt pnotes.txt > allnotes.txt #concatenate multiple files into one

tee — duplicate standard input

The tee utility copies standard input to standard output, making a copy in zero or more files.

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
ps aux | tee -a program.ps #use -a option to append to files rather than overwriting them

less — opposite of more

less is a program similar to more, but which allows backward movement in the file as well as forward movement. I find myself using less frequently to quickly read content of a file (often the linux config files).

Frequently used less options

+-------------+--------------------------------------------------------------------------------------------+
| Option | Description |
+-------------+--------------------------------------------------------------------------------------------+
| -i | Ignores case sensitivity during search. |
| -J | Displays a status column on the left side of the screen. |
| -n | Removes line numbers from the screen. |
| -N | Displays line numbers at the beginning of each line. |
| -p[pattern] | Instruct less to start at the first occurrence of the specified pattern in the input file. |
| -s | Merges consecutive blank lines into a single blank line. |
| -z[n] | Changes the default scrolling window size to the specified n lines. |
+-------------+--------------------------------------------------------------------------------------------+

Less keyboard shortcuts

+---------+----------------------------------------------------------------------+
| Key | Action |
+---------+----------------------------------------------------------------------+
| / | search for a pattern which will take you to the next occurrence. |
| n | for next match in forward |
| N | for previous match in backward |
| ? | search for a pattern which will take you to the previous occurrence. |
| n | for next match in backward direction |
| N | for previous match in forward direction |
| CTRL+F | forward one window |
| CTRL+B | backward one window |
| j | navigate forward by one line |
| k | navigate backward by one line |
| G | go to the end of file |
| g | go to the start of file |
| q or ZZ | exit the less pager |
+---------+----------------------------------------------------------------------+

Read more about less navigation here.

Examples:

less error_log
less -N -p '</VirtualHost>' /etc/apache2/httpd.conf #show line number and match pattern
exim -bp | less #redirect long command output to less
less --help #view less help page with list of all options

tail — display the last part of a file

The tail utility displays the contents of file or, by default, its standard input, to the standard output.

tail /var/log/nginx/error.log #show last 10 lines of data
tail -n 100 access.log #show last 100 lines of data
tail -100 access.log #show last 100 lines of data
tail +500 access.log #show data starting from line number 500 to last line
tail -f access.log #show last 10 lines and update when new line are added
#(view file content in realtime as new content is added mainly used to monitor log files)

head — display first lines of a file

This filter displays the first count lines or bytes of each of the specified files, or of the standard input if no files are specified. If count is omitted it defaults to 10.

head /var/log/syslog             #view first 10 lines 
head -n 40 /etc/nginx/nginx.conf #view first 40 lines

cp — copy files and directories

The cp utility copies the contents of the source_file to the target_file OR the contents of each named source_file is copied to the destination target_directory

cp notes.txt notes-backup.txt                        #copy file
cp notes.txt notes-backup.txt log.txt /backup/notes/ #copy multiple files to directory
cp *.txt /backup/notes/ #copy multiple files matching a pattern
cp -R /var/lib/mysql/ /backup/ #copy directory recursively
cp -pR /var/lib/mysql/ /backup/ #copy directory recursively and preserve file attributes
cp -v /backup/notes/* ~/notes/ #copy with verbose output

mv — move (rename) files

The mv utility is used to rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

mv notes.txt /backup/notes/                         #move a file
mv notes-backup.txt notes.txt #rename a file
mv /backup/notes/notes.txt ~/notes/old-notes.txt #move and rename a file
mv -i ~/all-notes/* . #confirm before overwriting existing files (use -f to force override)

mv note-jan.txt note-feb.txt note-march.txt /backup/notes/ #move multiple files
mv note*.txt /backup/notes/ #move using pattern

rm—remove files or directories

The rm utility attempts to remove the non-directory type files (default behavior) specified on the command line.

rm notes-old.txt                    #remove a file
rm -i notes-jan.txt notes-feb.txt #remove multiple files and ask before deleting each file
rm -f notes*.txt #forcly remove files matching a pattern
rm -r ~/notes/ #recursively remove directory and it's all content
rm -rf / #never run this command or if you are curious try it own your own risk

rmdir — remove empty directories

The rmdir utility removes the directories provided that they are empty.

rmdir notes/                 #remove empty directory
rmdir -p /home/demo/mydir #remove mydir, then demo and finally /home (all dir must be empty)

clear — clear the terminal screen

clear clears your screen if this is possible.

clear #clear the screen (keyboard shortcut ctrl + l)

man — display online manual documentation pages

The man utility finds and displays manual documentation pages. man opens documentation in less like viewer allowing all less shortcuts.

The sections of the manual are:
1. General Commands Manual
2. System Calls Manual
3. Library Functions Manual
4. Kernel Interfaces Manual
5. File Formats Manual
6. Games Manual
7. Miscellaneous Information Manual
8. System Manager’s Manual
9. Kernel Developer’s Manual

man ls       #view manual page of ls command
man intro #displays man page of intro in first section that command appears in (here it shows man of intro from section 1)
man 2 intro #specify the man section (man intro.2)
man man.7 #view manual of man command from section 7

In this series of articles I will list the linux commands that i find useful and frequently use. If you find any command that should be here let me know in the comments. Follow and subscribe my medium profile to get notified when I publish my future articles.

--

--

Sudarshan Pathak

I am a software developer, have learnt few things and learning new things regularly. I have started writing to share about technology and to keep learning.