Linux Basic Commands

Ravikant Kumar
devopsfunda
Published in
9 min readSep 11, 2017

In Linux ‘cd’ (Change Directory) command is one of the most important and most widely used command for newbies as well as system administrators.

Change from current directory to /usr/local.

Change current directory to parent directory.

# cd -

Change current directory to previous directory.

# cd ../../

Change current directory to two level up parent directory.

# cd ~ or 
# cd

Move user home from anywhere directory.

# cd /

Move to root directory.

pwd command

Linux pwd (print working directory) command displays your location currently you are working on. It will give the whole path starting from the root ending to the directory.

# pwd

ls command

# ls

ls with no option list files and directories in bare format where we won’t be able to view details like file types, size, modified date and time, permission and links etc.

# ls -l

Here, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or folder name and owner of file and it’s permission.

# ls -al

Here option -a include hidden file in long list way.

# ls -lh

With combination of -lh option, shows sizes in human readable format.

# ls -F

Using -F option with ls command, will add the ‘/’ Character at the end each directory.

# ls -r

The following command with ls -r option display files and directories in reverse order.

# ls -R

ls -R option will list very long listing directory trees. See an example of output of the command.

# ls -lt

With combination of -lt will shows latest modification file or directory date as top.

# ls -lS

With combination of -lS displays file size in order, will display big in size first.

# ls -l /tmp

With ls -l command list files under directory /tmp. Wherein with -ld parameters displays information of /tmp directory.

mkdir command

Create the DIRECTORY(ies), if they do not already exist.

# mkdir ravikant

It make ravikant directory in current directory.

# mkdir ravi certs video songs

It make multiple directory

# mkdir -p ravi/folder1/innerFolder

It make directory when parent directory is not exit. It also create parent directory as well.

# mkdir -m 777 mydir

Create the mydir directory, and set its permissions such that all users may read, write, and execute the contents. -m option for assign mode.

Touch command

The touch command is a standard program for Unix/Linux operating systems, that is used to create, change and modify timestamps of a file.

# touch myfile.txt

It create empty file

# touch myfile.txt index.html home.php

It create multiple file

Cat command

The cat (short for “concatenate”) command is one of the most frequently used command in Linux/Unix like operating systems. cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files.

# cat file.txt

It show contents of file in terminal

# cat file1 file2

It show contents of file1 and file2

# cat > testFile

It create testFile. And Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d’) to exit. The text will be written in testFile file.

# cat -n file.txt

It show contents of file with line number.

# cat -e file.txt

It show $ at the end of each line.

# cat test > test2

It redirect standard output of a file into a new file else existing file with ‘>’ (greater than) symbol. Existing contents of test1 will be overwritten by contents of test file.

# cat test >> test1

Appends in existing file with ‘>>’ (double greater than) symbol. Here, contents of test file will be appended at the end of test1 file.

# cat < test2

When you use the redirect with standard input ‘<’ (less than symbol), it use filename test2 as an input for a command and output will be shown in a terminal.

# cat test test1 test2 > test3

This will create a file called test3 and all output will be redirected in a newly created file.

# cat test test1 test2 test3 | sort > test4

This will create a file test4 and output of cat command is piped to sort and result will be redirected in a newly created file.

cp command

Copy a file or directory.

# cp origfile newfile

This make a copy of origfile to filename newfile. It also overwrite when newfile is exit.

If you want to prompted when overwrite than use -i option with cp command.

# cp origfile /directory/subdir/.

Creates a copy of the file in the working directory named origfile. The copy will be located in the directory /directory/subdir, and will be named origfile.

# cp origfile /directory/subdir/newfile

Copy origfile to given directory with new file name .

# cp *.jpg /home/ravi/

Copy all file having extension .jpg to /home/ravi/ directory.

# cp -R mydir /home/ravi/

It copy entire directory structure to /home/ravi/ directory.

mv command

Linux mv command is used to move files and directories from one location to another. Apart from moving the files, it can also rename a file or directory.

#mv dir newdir# mv filename newfilename

This command rename the directory/file. While renaming a file using mv command, it keeps the inode number same even after moving it to a different name. If you move the file to a different filesystem, the inode number will be different.

# mv -u dir/* dir2/

When you do mv *, it will move all the files to the destination directory. However, if you want to move only the files from the source directory that don’t exist in the destination directory, use the mv -u option as shown above.

rm command

# rm filename# rm file1 file2 file3

It remove file and files.

# rm -f file

If remove force fully whether write-protected or not, without prompting the user. It does not display an error message or return error status if a specified file does not exist.

# rm -i file

The (i.e., interactive ) option tells rm to prompt the user for confirmation before removing each file and directory. If both the -f and -i options are specified, the last one specified takes effect.

# rm -r dir

The -r option is used for removing directory.

# rm -r -- -ravi

Remove “-ravi” directory ‘-’ is used.

rmdir command

The rmdir utility removes the directory entry specified by each directory argument , provided the directory is empty.

# rmdir dir

Remove ‘dir’ directory when it is empty.

clear command

‘clear’ clears your screen if this is possible, including its scrollback buffer (if the extended “E3” capability is defined). clear looks in the environment for the terminal type and then in the terminfo database to determine how to clear the screen.

# clear

top command

top command displays processor activity of your Linux box and also displays tasks managed by kernel in real-time. It’ll show processor and memory are being used and other information like running processes.

# top# top -u apache

Show user specific processes.

Press (Shift+P) to sort processes as per CPU utilization. See screenshot below.

Press ‘q’ to quit exit the window.

Press ‘z’ option in running top command will display running process in color which may help you to identified running process easily.

In ubuntu option ‘E’ is used for changing memory size format.

ps command

The ps command displays active processes.

# ps -ef

Option -e for show all process and -f for full listing.

kill command

Kill command is use to send signal to a process or to kill a process.

# kill -9 1432

It kill process which id is 1432.

# kill -9 -l

Kills all processes possible to be killed.

Use -l or -L to list available signals. Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be specified in three ways: -9, -SIGKILL or -KILL. Negative PID (process ID) values may be used to choose whole process groups

Command for start, stop, or restart of services.

# sudo /etc/init.d/<servic_name> <option># sudo /etc/init.d/mysql restart

These command is used for start/stop/restart of mysql service. You can change according to your service.

# sudo service <service_name> <start/stop/restart/reload># sudo service httpd restart

This command restart the httpd server.

# sudo systemctl start nginx

This command is used to start nginx server.

grep command

grep, which stands for “global regular expression print,” processes text line by line and prints any lines which match a specified pattern. grep is a powerful file pattern searcher that comes equipped on every distribution of Linux.

Grep is a powerful tool for matching a regular expression against text in a file, multiple files, or a stream of input. It searches for the of text that you specify on the command line, and outputs the results for you.

# grep "ravikant.ravi672@gmail.com" index.html

Above command search the email in index.html page.

# grep "Ravikant kumar" --color -n -i employee.txt

Above command search the name and show in different color with line no (option -n). Option -i is used for case-insensitive.

# grep "Square Yard" --color -n -r *

Above command search pattern in recursive way( option -r). That means it search in directories as well as files.

The grep can be very useful for filtering from stdout.

# find . -name ".mp3" | grep -i JayZ | grep -v -i "remix"

Above command find all of the *.mp3 files from the artist JayZ, but you don’t want any of the remixed tracks. Option -v is for invert the sense of matching, to select non-matching lines.

find command

Find command used to search and locate list of files and directories based on conditions you specify for files that match the arguments. Find can be used in variety of conditions like you can find files by permissions, users, groups, file type, date, size and other possible criteria.

# find . -name index.html

Above command find index.html file in current directory.

# find /home/rkumar/ -iname ravi.txt

Above command find file specified file with ignoring case sensitive (option -i).

# find / -type d -name Ravikant

Above command find Ravikant directory.

# find . -type f -name *.php

Above command find all file end with .php in current directory.

# find . -type f -perm 0777 -print

Find all the files whose permissions are 777.

# find /app/web/ -type f -perm 0777 -print -exec chmod 755 {} \;

Find file with permission 777 and change permission to 755.

# find /app/backup/ -mtime 10 -exec rm -rf {} \;

Above command find files which are modified 10 day back and remove them.

-atime is for accessed time.

# find / -mtime +50 -mtime -100

Above command find all the files which are modified more than 50 days back and less than 100 days.

# find / -mmin -60 -size -100K

Above command find files which are modified within 1 hour whose size within 100K

# find /music/ -type f -name *.mp3 -size +10M -exec rm {} \;

Above command find mp3 file whose size is greater than 10MB and remove them.

sort command

Sort is a Linux program used for printing lines of input text files and concatenation of all files in sorted order. Sort command don’t actually sort the files but only print the sorted output, until your redirect the output.

# sort empName.txt

Above command output in sorted order.

# sort -r empName.txt > newEmpName.txt

Above command sort list in reverse order and save to newEmpName.txt file.

Option -r is used for reverse order.

Option -u is used for unique sort ( remove duplicate).

sed command

Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

# sed -n '22,30p' database.php

Above command view range of line no 22 to 30. Option ‘p’ is for print

# sed '10,20d' database.php

Above command print file without 10 to 20 line. Option d for delete.

# sed -n -e '7,15p' -e '30,40p' myfile.txt

Above command display lines 7–15 and 30–40 from myfile.txt. Option -e for add the script to the commands to be executed

# sed 's/localhost/127.0.0.1/g' database.php

Above command search pattern localhost and replace it to 127.0.0.1 in file. Option s for search & replace and g for copy/append. Also we use gi option for ignore case sensitive.

# sed -i '11, 15 s/^/#/' /app/web/WEB-INF/db.properties

Above command add # at start of line 11 to 15. Caret sign (^) represent beginning of line.

# sed -i '16, 19 s/^#//' /app/web/WEB-INF/db.properties

Above command remove # from start of line 16 to 19. Option i is used for edit and save file.

Originally published at http://devopsfunda.com on September 11, 2017.

--

--