You are on page 1of 12

-------------------------------------------------------------------------------------------

UNIX Helpful Commands


-------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------
Brush up Basic Commands
--------------------------------------------------------------------------------------------------

 ls --- lists your files


ls -l --- lists your files in 'long format', which contains lots of useful information, e.g. the exact size of
the file, who owns the file and who has the right to look at it, and when it was last modified.
ls -a --- lists all files, including the ones whose filenames begin in a dot, which you do not always
want to see.
There are many more options, for example to list files by size, by date, recursively etc.
 more filename --- shows the first part of a file, just as much as will fit on one screen. Just hit the
space bar to see more or q to quit. You can use /pattern to search for a pattern.
 mv filename1 filename2 --- moves a file (i.e. gives it a different name, or moves it into a different
directory
 cp filename1 filename2 --- copies a file
cp -i [ and the -i ensures that if there is another file named meeting1 in the current directory, you will
not overwrite it by accident.]

 rm filename --- removes a file. It is wise to use the option rm -i, which will ask you for confirmation
before actually deleting anything.

 diff filename1 filename2 --- compares files, and shows where they differ

 wc filename --- tells you how many lines, words, and characters there are in a file

 chmod options filename --- lets you change the read, write, and execute permissions on your files.
The default is that only you can look at them and change them, but you may sometimes want to
change these permissions. For example, chmod o+r filename will make the file readable for
everyone, and chmod o-r filename will make it unreadable for others again.
 mkdir dirname --- make a new directory
 cd dirname --- change directory.
'cd' without arguments - getting into users home directory type :
& 'cd ..' will get you one level up from your current position.
& 'cd -' will get your previous directory who were in.

 pwd --- tells you where you currently are

 File Compression
o gzip filename --- compresses files, so that they take up much less space. Usually text files
compress to about half their original size, but it depends very much on the size of the file and
the nature of the contents. There are other tools for this purpose, too (e.g. compress), but gzip
usually gives the highest compression rate. Gzip produces files with the ending '.gz' appended
to the original filename.
o gunzip filename --- uncompresses files compressed by gzip.
o gzcat filename --- lets you look at a gzipped file without actually having to gunzip it (same
as gunzip -c). You can even print it directly, using gzcat filename | lpr
 passwd --- lets you change your password

 ps -u yourusername --- lists your processes including the process ID, which you need if you have to
kill a process.
Those may include the shell (tcsh or whatever you're using), and anything you were running,
Be careful not to kill your current shell - the one with the number closer to the one of the ps command
you're currently running.

 kill PID --- kills (ends) the processes with the ID you gave. This works only for your own processes,
of course. Get the ID by using ps. If the process doesn't 'die' properly, use the option -9. But attempt
without that option first, because it doesn't give the process a chance to finish possibly important
business before dying.

 quota -v --- show what your disk quota is (i.e. how much space you have to store files), how much
you're actually using, and in case you've exceeded your quota (which you'll be given an automatic
warning about by the system) how much time you have left to sort them out (by deleting or gzipping
some, or moving them to your own computer).

 du filename --- shows the disk usage of the files and directories in filename (without argument the
current directory is used). du -s gives only a total.

 df - This command reports file system disk usage (i.e., the amount of space taken up on mounted file
systems). For each mounted file system, df reports the file system device, the number of blocks used,
the number of blocks available, and the directory where the file system is mounted.

---------------------------------------------------------------------------------- Usefull
Commands - Helpful for Interviews and in Project ---------------------------------------------
-------------------------------------

rm command - Removing files and directory -

$ ls -lrt

total 0

-rw-r--r-- 1 Ankit None 0 Feb 13 01:13 abc.txt

-rw-r--r-- 1 Ankit None 0 Feb 13 01:13 sales.txt

drwxr-xr-x+ 1 Ankit None 0 Feb 13 01:13 myWork

drwxr-xr-x+ 1 Ankit None 0 Feb 13 01:28 temp

1. Removes file "abc.txt"


$ rm abc.txt

---------------------------

2. Removes all the files in the working directory, but not the sub-directories..

$ rm *

---------------------------

3. Removes files(abc.txt,sales.txt) and sub-directory(myWork) present in current


directory

$ rm -r *

---------------------------

4. Remove only the subdirectory

$ rm -r myWork

---------------------------

5. Removing an empty directory. Even rm -r also doesnt work

$ rmdir temp or $ rm -d temp

---------------------------

6. Removing an file that start with '-' like -file1.txt

$ rm -- -file1.txt

------------------------------------------------------------------------------

ls command - listing files and directory


ls -ld *
List all the file and directory names in the current directory using long format.
With the "d" option, ls just lists them like regular files.

Ankit@Ankit-PC ~
$ ls -ld *
drwxr-xr-x+ 1 Ankit None 0 Feb 7 23:40 dir1
drwxr-xr-x+ 1 Ankit None 0 Feb 7 23:40 dir2
-rw-r--r-- 1 Ankit None 0 Feb 7 23:51 file
drwxr-xr-x+ 1 Ankit None 0 Jan 13 20:13 mywork
drwxr-xr-x+ 1 Ankit None 0 Jun 14 2014 test

Without the "d" option, ls would list the contents of any sub-directory of the
current.

ls -l *

Ankit@Ankit-PC ~
$ ls -l *
-rw-r--r-- 1 Ankit None 0 Feb 7 23:51 file

dir1:
total 0
-rw-r--r-- 1 Ankit None 0 Feb 7 23:40 dir1_file1
drwxr-xr-x+ 1 Ankit None 0 Feb 7 23:52 dir1a
dir2:
total 0

mywork:
total 2
-rw-r--r-- 1 Ankit None 123 Jan 13 20:13 file1
-rw-r--r-- 1 Ankit None 21 Jun 22 2014 file2
-rw-r--r-- 1 Ankit None 0 Jun 22 2014 file3
-rw-r--r-- 1 Ankit None 0 Jun 22 2014 file4

---------------------------

List only the directories in the current directory


ls -l | grep ^d

Ankit@Ankit-PC ~
$ ls -l | grep ^d
drwxr-xr-x+ 1 Ankit None 0 Feb 7 23:40 dir1
drwxr-xr-x+ 1 Ankit None 0 Feb 7 23:40 dir2
drwxr-xr-x+ 1 Ankit None 0 Jan 13 20:13 mywork
drwxr-xr-x+ 1 Ankit None 0 Jun 14 2014 test

--------------------------------------------------------------------------------------------------------------------
GREP Command -
The name GREP is the abbreviation of Global Regular Expression Pattern. Yep! it’s pattern search tool
under Unix.

Sample file - file2.txt


Ankit@Ankit-PC ~
$ cat file2.txt
My name is ankit
Your user-name is Ankit
Marine drive is in mumbai
unix and informatica
unixcommands
---------------------------
Search for string "ankit" in file
Ankit@Ankit-PC ~
$ grep "ankit" file2.txt
My name is ankit
---------------------------
Case in-sensitive Search
Ankit@Ankit-PC ~
$ grep -i "ankit" file2.txt
My name is ankit
Your user-name is Ankit
---------------------------
To print the filename along with match; if multiple file names are given ..you will
get match along with file name
Ankit@Ankit-PC ~
$ grep "ankit" file2.txt file3.txt
file2.txt:My name is ankit
file3.txt:welcome ankit
or
$ grep "ankit" file2.txt /dev/null
or
$ grep -H "ankit" file2.txt
file2.txt:My name is ankit
---------------------------
Search Checking for full word not substring
Ankit@Ankit-PC ~
$ grep -w "unix" file2.txt
unix and informatica
---------------------------
Normal Search
Ankit@Ankit-PC ~
$ grep "unix" file2.txt
unix and informatica
unixcommands
---------------------------
Search in all files inside a directory- Recursive searching
Ankit@Ankit-PC ~
$ grep -r "unix"
---------------------------
Exclude pattern match - For eg. pattern "unix" in file2.txt
Ankit@Ankit-PC ~
$ grep -v "unix" file2.txt
My name is ankit
Your user-name is Ankit
Marine drive is in mumbai
---------------------------
Counting the no. of matches
Ankit@Ankit-PC ~
$ grep -c "mumbai" file2.txt
1
---------------------------
To display the filenames that contains pattern
Ankit@Ankit-PC ~
$ grep -l "unix" file*
file1.txt
file2.txt
---------------------------
Searches all the patterns that starts with "My" and ends with "it"
$ grep -i "My.*it" file2.txt
My name is ankit
---------------------------
Showing Line number while displaying the output
Ankit@Ankit-PC ~
$ grep -n "unix" file2.txt
4:unix and informatica
5:unixcommands
---------------------------
(^) used to represent start of the line with pattern "unix" in below e.g
Ankit@Ankit-PC ~
$ grep "^unix" file2.txt
unix and informatica
unixcommands
---------------------------
($) used to represent end of the line with pattern "it" in below e.g
Ankit@Ankit-PC ~
$ grep "it$" file2.txt
My name is ankit
Your user-name is Ankit
---------------------------
Search for pattern that starts with numeric digits
Ankit@Ankit-PC ~
$ grep "^[0-9]" file1.txt
123 PUNE
456 GOA
--------------------------------------------------------------------------------------------------------------------
find command - find is one of the powerful utility of Unix (or Linux) used for searching the files in a
directory hierarchy. The syntax of find command is

Ankit@Ankit-PC ~
$ ls -l
total 6
drwxr-xr-x+ 1 Ankit None 0 Feb 7 23:40 dir1
drwxr-xr-x+ 1 Ankit None 0 Feb 7 23:40 dir2
-rw-r--r-- 1 Ankit None 107 Feb 8 15:02 file1.txt
-rw-r--r-- 1 Ankit None 101 Feb 8 14:36 file2.txt
drwxr-xr-x+ 1 Ankit None 0 Jan 13 20:13 mywork
drwxr-xr-x+ 1 Ankit None 0 Jun 14 2014 test

To find all the files with name "file1.txt" in current & sub-dir
Ankit@Ankit-PC ~
$ find -name "file1.txt"
./file1.txt
./mywork/file1.txt
---------------------------
To find for a file in current directory only
Ankit@Ankit-PC ~
$ find -maxdepth 1 -name "file1.txt"
./file1.txt
---------------------------
To find for files containing a specific word in its name
Ankit@Ankit-PC ~
$ find -name "*file*.txt"
./file1.txt
./file2.txt
./mywork/file1.txt
---------------------------
To find for files in specific directories for e.g "mywork"
$ find mywork -name "*file*.txt"
---------------------------
List the directories and its subdirectories in current directory
Ankit@Ankit-PC ~
$ find . -type d
./dir1
./dir1/dir1a
./dir1/dir1a/infa
./dir2
./mywork
./test
---------------------------
List the directories present in current dir
Ankit@Ankit-PC ~
$ find . -maxdepth 1 -type d
.
./dir1
./dir2
./mywork
./test
---------------------------
List the directories present in current dir and 1-level down to current dir
Ankit@Ankit-PC ~
$ find . -maxdepth 2 -type d
.
./dir1
./dir1/dir1a
./dir2
./mywork
./test
---------------------------
List the files present in current dir and its sub-dir
Ankit@Ankit-PC ~
$ find . -type f
---------------------------
List the files at 1-level down to current dir
Ankit@Ankit-PC ~
$ find . -maxdepth 1 -type f
---------------------------
List the files owned by a user called Ankit
Ankit@Ankit-PC ~
$ find . -user Ankit
---------------------------
List the empty files in a current directory and subdirectories ,f - files & d -
directories
$ find . -type f -empty
---------------------------
Find files greater than 10M size
$ find . -size +10KB
---------------------------
Find files which are modified within 10 MINS
$ find . -type f -mmin -10
---------------------------
Find files according to days use : option -mtime
$ find . -type f -mtime -2
"-mtime -2" means files that are less than 2 days old, such as a file that is 0 or 1
days old.
"-mtime +2" means files that are more than 2 days old... {3, 4, 5, ...
---------------------------
Files which are changed within 2days use : option -ctime
$ find . -type f -ctime -2
---------------------------
Files which are accessed within 1day use : option -atime
$ find . -type f -atime -1
---------------------------
Explanation -
find . -mtime 0 # find files modified between now and 1 day ago
# (i.e., within the past 24 hours)
find . -mtime -1 # find files modified less than 1 day ago
# (i.e., within the past 24 hours, as before)
find . -mtime 1 # find files modified between 24 and 48 hours ago
find . -mtime +1 # find files modified more than 48 hours ago

find . -mmin +5 -mmin -10 # find files modified between


# 6 and 9 minutes ago
---------------------------
Giving the output of "find" command to other commands -
Find "file1.txt" in current directory and display its permissons(long list format)
Ankit@Ankit-PC ~
$ find -name "file1.txt" -exec ls -l {} \;
-rw-r--r-- 1 Ankit None 107 Feb 8 15:02 ./file1.txt
-rw-r--r-- 1 Ankit None 123 Jan 13 20:13 ./mywork/file1.txt

or (alternate command)
$ find -name "file1.txt" | xargs ls -l
---------------------------
Find the filesNames which contains the name "file" in it & then display only the files
which contains "unix" in them

$ find -name "*file*" -exec grep -H "unix" {} \;


./file1.txt:Hi welcome to unix
./file2.txt:unix and informatica
./file2.txt:unixcommands
./mywork/file1.txt:welcome to unix
./mywork/file2:ankit welcome to unix
---------------------------
Remove files that contain file2 in filename option -mtime
$ find . -type f -name "*file2*" -exec rm -r {} \;
---------------------------
Head & Tail Command

To print first line of a file , or header of a file

$> head -1 file.txt

To print first 3 records of a file , head –n filename


$> head -3 file.txt

If a file has 10 records , then it skip last 3 records of file and print 1-7 records
$> head –n -3 file.txt

Print first 10 records by default, if used without any option


$> head file.txt

To get last records from a file or footer of a file


$> tail -1 file.txt
Similary tail –3 filename return last 3 records of a file.

If a file has 10 records, then records from 3 to 10 will come


$> tail –n +3 file.txt

If used without any option, By default last 10 records will come


$> tail file.txt

To display a particular record , say to display 6th record of a file


$> head -6 file.txt | tail -1
Or
$> tail –n +6 file.txt | head -1

Print record between 6 and 10


$> head -10 file.txt | tail -4
SED Command -
Sed command can be used to delete or remove specific lines which matches a given pattern
or in a particular position in a file.

-n, --quiet, --silent [ suppress automatic printing of pattern space ]


Use the -n option along with the /p print flag to display only the replaced
lines.

Range Description

p Prints the line

d Deletes the line

s/pattern1/pattern2/ Substitutes the first occurrence of pattern1 with pattern2.

To remove the header from file, or first line from a file.


Note: It does not delete from a file, only there is a change in display output
sed '1d' file.txt

To change the file in place, use an option „-i‟


Sed –i „1d‟ file.txt

To display first line of a file, (Last line is represented by $ )


This will delete all the records from display output from line no. 2 to last line of
the file
sed '2,$ d' file.txt

To remove last line/trailer from a file


Sed –i „$ d‟ file.txt

To remove certain lines from a file. Delete line 5 to line 7 from file
sed –i '5,7 d' file.txt

To print n-th line of file, Prints 4th line of file


Sed –n „4 p‟ file.txt

To print last line of a file.


Sed –n „$ p‟ file.txt

To display line which contain “city”


Sed –n „/city/p‟ file.txt

To delete lines which contains say “city”


Sed –i „/city/d‟ file.txt
To replace a string with another string ( city with Mumbai )
Sed –i „s/city/mumbai‟ file.txt

Only the first occurrence will be substituted.


In order to substitute all occurrences use „g‟ - global
Sed –i „s/city/Mumbai/g‟ file.txt

To substitute all occurrences at 6th line


Sed –i „6s/city/Mumbai/g‟ file.txt

To substitute all occurrences from 2 to 5th line


Sed –i „2,5s/city/Mumbai/g‟ file.txt

Matches all the lines starting with „city‟ and then deleting them
Sed –i „/^city/d‟ file.txt

/a.c/ Matches lines that contain strings such as a+c, a-c, abc, match, and a3c, whereas the pattern

/a*c/ Matches the same strings along with strings such as ace, yacc, and arctic.

/[tT]he/ Matches the string The and the:

/^$/ Matches Blank lines

/^.*$/ Matches an entire line whatever it is.

/ */ Matches one or more spaces

Deletes the lines which ends with „os‟ and display the output
cat file1.txt | sed '/os$/d'

Files starting with either 1 or 2


ls | sed –n '/^[21]/d'

Files starting only with 12


ls | sed –n '/^[1][2]/d'

TO list files which start only with „12‟


$ ls | sed '/^[1][2]/p'

To list lines which doesn‟t contains „unix‟


$ awk '!/unix/' file1.txt
hi ankit
hi you there
this is mouse

To list lines which contains „unix‟


$ awk '/unix/' file1.txt
welcome to unix
unix is os

You might also like