30 Basic UNIX Commands List
30 Basic UNIX Commands List With Examples is a good reference for someone who is beginner to UNIX/LINUX Operating System. These are most commonly used and basic Unix commands listed below:
- Man (Help Command)
man is the interface used to view the system’s reference manuals. it can be used to display manual pages, scroll up and down, search for occurrences of specific text, and other useful functions.
Example 1:
man cat
This command will show you the manual page for the cat command, like syntax and the usage of the cat command.
Example 2:
man -k search
This command can be used to search for the text “search” in all manual pages
2. cat
cat command is used to concatenate or displays the contents of a file.
Displaying the contents of file:
cat filename
— Displays the content of filename
Displaying the contents of file (including special characters):
cat –v filename
— Displays the content of filename including special characters like, control characters.
Displaying the contents of file with line numbers:
cat –n filename
— Displays the content of filename with the line numbers at the beginning.
Copy file content to new File:
Cat filename1 > filename2
— filename1 content is copied to filename2(filename2 is truncated before copying)
Appending one file content to another:
cat filename1 >> filename2
— filename1 content is appended to the end of filename2.
Creating file:
cat > filename
— Enter the text, once all the text is entered press cntrl + d, which saves the file content.
Appending to existing File:
cat >> filename
— Enter the text, it will append the content to the end of the filename , once all the text is entered press cntrl + d, which saves the file content.
3. passwd
passwd command is used to change the password associated with our individual account name.
When you type this command it will ask you for the old password and the new password.
4. pwd
pwd (present working directory) tells you your current directory.
5. cd
cd is used to change directories.
cd new-directory
Change the current directory to new-directory
“cd ~” or simply “cd”
Change the current directory to users home directory.
cd ~username
Change the current directory to username’s home directory.
cd –
For changing directory to previous working directory
cd ..
Change working directory to parent directory.
cd <pattern1> <pattern2>
To change the current directory to other directory with only small difference in one of the intermediate directory name
Ex: If the current directory is “/user/path01/dir”
to change the directory to “/user/path02/dir” use this command
cd 01 02
6. mkdir
mkdir (make directory) is used to create a new directory,
It can take more than one parameter, interpreting each parameter as another directory to create.
mkdir Dirname
To create the directory Dirname
mkdir -m <Permission> Dirname
To create the directory Dirname with the given permission
Ex: mkdir -m 777 Dirname
7. rmdir
rmdir (remove directory) is used to remove a directory,
rmdir Dirname
Deletes the empty directory Dirname can be used only when the directory is empty.
If the directory is not empty the below command can be used to delete the directory recursively
rm –r dirname
8. cp
cp is used to copy contents of file1 to file2
cp file1 file2
contents of file1 is copied to file2 in the same directory
cp folder1/file1 folder2
contents of file1 is copied to file1 in the inside of folder2 directory
cp –r dir1 dir2
To copy directory from one location to another.
9. rm
rm is used to remove a file.
rm filename
removes a file named filename
rm –f filename
-f option is used to remove the file forcefully.
rm –r dirname
to remove the nonempty directory “dir”
10. mv
mv is used to move a file.
mv filename1 filename2
mv command moves the file from filename1 to filename2.
If the filename1 and filename2 are pointing to the same directory then it renames the file filename1 to filename2.
11. chmod
Chmod (change mode) is used to change the permissions on a file.
There are two ways to do it.
chmod [number][number][number] filename
Number = (read)4 + (write)2 + (execute)1
Example: chmod 754 filename
for owner: read, write and execute permissions (4+2+1)
for group: read and execute permissions (4+0+1)
for others: only read permission (4+0+0)
chmod [u/g/o/a][+/-/=] filename
u=owner, g=group, o=others, a=all
“+”=Add permission, “-”=remove permission, “=“=assign permission
Example1: chmod a+r filename
above command adds the read permission to all the users.
Exmple2: chmod o-w filename
above command removes the write permission from other users.
12. du
du (disk usage) will count the amount of disk space for a given directory, and all its subdirectories take up on the disk.
Example:
du –h dirname
Above command gives disk space for dirname, and all its subdirectories in the human readable format.
13. df
df (disk filling) summarizes the amount of disk space in use. For each file system, it shows the total amount of disk space, the amount used, the amount available, and the total capacity of the file system that’s used.
Example :
df –kh dirname
Above command gives total amount of disk space, the amount used, the amount available, and the total capacity of the file system that’s used for dirname.
14. more
more is a command to view (but not modify) the contents of a text file one screen at a time.
Example 1:
more filename
Use space button to scroll down, and press “q” to stop viewing the contents.
Example 2:
more -2 filename
This command can be used to view two lines at a time(you can change this to other numbers too).
15. less
less is a command similar to more, but which allows backward movement in the file as well as forward movement.
Example 1:
less filename
Use the arrow buttons to scroll.
16. head
head will display the first lines in the listed files, by default it displays first 10 lines of the file .
example1:
head filename
This command displays first 10 lines of the filename
example1:
head -25 filename
This command displays first 25 lines of the filename.
17. tail
tail will display the last lines in the listed files, by default it displays last 10 lines of the file .
example1:
tail filename
This command displays last 10 lines of the filename.
example1:
tail -25 filename
This command displays last 25 lines of the filename.
18. grep
grep is the generalized regular expression parser.
This command is used to search for the pattern in the file.
Example 1:
grep “pattern“ filename
This command displays all the lines in the filename which contains the pattern “pattern” (case sensitive).
Example 2:
grep -i “pattern“ filename
This command displays all the lines in the filename which contains the pattern “pattern” (case insensitive).
Example 3:
grep –c “pattern“ filename
This command displays the count of the lines having the pattern “pattern”
Example: 4
grep -v “pattern“ filename
This command displays the lines of the file which does not have the pattern “pattern”.
Example 5:
grep -l “pattern“ *
This command displays the filenames in the current directory which has the pattern “pattern“.
19. wc
wc (word count) simply counts the number of words, lines, and characters in the file(s).
wc [-option] filename
The three parameters, clw, stand for character, line, and word respectively, and tell wc which of the three to count.
Example :
wc –l filename
This command will display the number of the lines in filename.
20. cmp
cmp compares two files. The first must be listed on command line, while the second is either listed as the second parameter or is read in form standard input.
cmp is very simple, and merely tells you where the two files first differ.
Example :
filename1 filename2
21. diff
diff utility is a data comparison tool that calculates and displays the differences between two files.
Example:
diff file1 file2
The first line of the diff output will contain:
1.line numbers corresponding to the first file,
2.a letter (a for add, c for change, or d for delete), and
3.line numbers corresponding to the second file.
Example:
“2a3” means: “After line 2 in the first file, a line needs to be added: line 3 from the second file.”
“4d3” means: “You need to delete line 4 in the first file so that both files sync up at line 3.”
“2,4c2,4” means: “Lines 2 through 4 in the first file need to be changed in order to match lines 2 through 4 in the second file.
22. gzip/gunzip/zcat
gzip:
gzip compresses files, it deletes the files specified on the command line and replaces them with files that have an identical name except that they have “.gz” appended to them.
Example:
gzip filename
gunzip:
gunzip uncompresses a file that was compressed with “gzip” or “compress”.
Example:
gunzip filename.gz
zcat (to view the data in compressed file):
zcat uncompresses either a list of files on the command line or its standard input and writes the uncompressed data on standard output.
Example:
zcat filename.gz
23. tr
The “translate characters” command operates on standard input-it doesn’t accept a filename as a parameter.
It replaces all occurences of first parameter with the second parameter.
Example 1:
tr “r” “p”
This command replaces the character “r” with “p.
Example 2:
tr “[:lower:]” “[:upper:]“
This command converts lower case letters to upper case.
Example 3:
tr -d “pattern“
This command delete characters in pattern from the input.
24. scp
scp stands for “secure copy”, and is used to copy the files between two different servers.
Syntax:
scp filename user_id@rem_servername:remote_dir_path
Example:
scp file1 trenovision@rserver:/home/user/trenovision
This command copies the file “file1” from the current server to the “rserver” server and to the path “/home/user/trenovision”.
25. date
Different usage with Output:
To get current data with Timestamp and time in Unix/Linux/Bash/Shell scripts.
date
Sun Mar 29 02:10:38 EDT 2015
date +%Y%m%d%H%M%S
20150329021052
date “+%Y-%m-%d %H:%M:%S.%N”
2015-03-29 02:11:12.784721000
Bash Date Formats:
- Y= Year in YYYY format
- m=Month in MM format
- y=Year in YY format
- d=day in DD format
- H=Hour in HH format
- M=Minute in MM format
- S=Seconds in SS format
- N=Nano seconds in NNNNNNNN format
Subtracting dates with output:
date -d”1 day ago“ — for taking yesterdays date and time.
Sat Mar 28 02:15:32 EDT 2015
(current date is “Sun Mar 29 02:15:32 EDT 2015 “)
date -d”1 hour ago“ — for taking 1hour ago time
Sun Mar 29 01:17:40 EDT 2015
26. sed
sed command is used to replace the text in a file.
Example 1:
sed ‘s/oldvalue/newvalue/g’ filename
Above command will replace all occurrence of the oldvalue with newvalue in the file filename(file content is modified) and will display it on standard output.
Example 2:
sed –I ‘s/oldvalue/newvalue/g’ filename
Above command will replace all occurrence of the oldvalue with newvalue in the file filename(file content is modified).
Example 3:
sed ‘/^$/d’ filename
This command will delete the empty lines in the file(file content is not changed) and displays the result on the standard output.
27. awk
awk is one of the most powerful tools in Unix used for processing the rows and columns in a file. Awk has built in string functions and associative arrays. Awk supports most of the operators, conditional blocks, and loops available in C language
Example1 :
awk -F “_” ‘{print $1}‘ filename
This command displays the first column data in the file filename(“_” is the column delimiter).
Example 2 :
awk ‘{ if($2 == “pattern”) print $0;}’ filename
This awk command checks for the string “pattern” in the 2nd column and if it finds a match then it will print the entire line.
28. touch
touch is a standard Unix command-line interface program which is used to update the access date and / or modification date of a file or directory. If the file does not exist then it creates a zero byte file.
Example1 :
touch filename
This command will update the timestamp of the file filename.
Example 2:
touch -t 1502250512 filename
Above command updates the timestamp of the file filename to year 15, month 02, day 25, hour 05 and minute 12.
29. Connecting to db2
Below example will explain you how to connect to db2 through Unix script.
db2 connect to database user username using password
db2 -xr filepath “sql query”
db2 terminate
After running the above command you will get the “sql query“ results in the file “filepath“.
29. Connecting to Teradata
Below example will explain you how to connect to Teradata through Unix script.
bteq<<EOF
.SET ERROROUT STDOUT;
.logon databasename/username,password
.export report file=filepath
Select query;
.export reset
.logoff
.quit
EOF
Above command will connect to Teradata database and will run the sql query “Select query” and will store the result in the file “filepath”.
30. tar
The command “tar” stands for tape archive. The tar command used to rip a collection of files and directories into highly compressed archive file.
Example 1:
tar -cvf TarName.tar DIRNAME
This command will create the tar file for the directory DIRNAME.
Example 2:
tar -xvf TarName.tar
This command will untar the tar file TarName.tar
and get the directory DIRNAME content to the current directory.
31. finger
finger is a command to find information about computer users. It usually lists the login name, the full name, and possibly other details about the user you are fingering.
Example 1:
finger username
Displays the information about the user username
Example 2:
finger -s trenovision
Displays the information of the all the users who has the name trenovision.
32. mailx
mailx command is used to send the mail to a given set of users.
Example 1:
cat filename | mailx –s “Subject” abcd@abc.com
This command will send the mail to abcd@abc.com with the subject “Subject” and the mail body as the conectn of the file “filename”
Example 2:
(cat mailbody.txt; uuencode attachmnt.txt attachmnt.txt ) | mailx -s “Subject” attachmnt.txt
This command can be used to send the mail with mail body subject and the attachment.
33. ftppwd
ftppwd command can be used to set and view the password for the given username and database combination in the environment, setting password does not mean that it will change the users password, it means that it will set it in the environment and it can be re fetched using the same command. Only the user who set this password in his environment can see the password.
Note:
If the user changes the password for the username in the given database, it is necessary that he needs to set the password again using ftppwd command, otherwise it will reflect the old password only.
Example 1:
ftppwd -s <PASSWORD> <DATABASE_NAME> <USER_NAME>
This command will set the password PASSWORD for the user name USER_NAME for the database DATABASE_NAME in the environment.
Example 2:
ftppwd <DATABASE_NAME> <USER_NAME>
This command is used to fetch the password for the username USER_NAME in the database DATABASE_NAME which was set using ftppwd –s option.
34. find
The find command is used to search any set of directories you specify for files that match the supplied search criteria. You can search for files by name, owner, group, type, permissions, date, and other criteria. The search is recursive in that it will search all subdirectories too.
Example 1:
find
This command lists out all the files in the current directory as well as the subdirectories in the current directory.
Example 2:
find directory_path -name “filename”
This command searches for file filename in the directory_path and its sub directory. Use –iname if you want to make search as case insensitive.
Example 3:
find -name ‘*.doc’ -o -name ‘*.txt’
This command can be used to search two different kind of files.
Example 4:
find ./test -type d -name “abc*”
This command can be used to find only the directoies.
Example 5:
find . -mtime 10
This command can be used to find all the files which are modified 10 days back.
35. alias
The alias command allows you to make new shortcuts and synonyms for commonly used commands.
Example 1:
alias lt=”ls -lrt | tail”
After running this command, if you type just “lt” it gives you the result of “ls –lrt | tail”.
Example 2:
alias day_ago=”date -d’1 day ago’
After running the above command if you type “day_ago” it returns the yesterdays date.