Professional Documents
Culture Documents
--------------------------------------------------------------------------------------------------
Brush up Basic Commands
--------------------------------------------------------------------------------------------------
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.
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 ---------------------------------------------
-------------------------------------
$ ls -lrt
total 0
---------------------------
2. Removes all the files in the working directory, but not the sub-directories..
$ rm *
---------------------------
$ rm -r *
---------------------------
$ rm -r myWork
---------------------------
---------------------------
$ rm -- -file1.txt
------------------------------------------------------------------------------
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
---------------------------
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.
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
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
If a file has 10 records , then it skip last 3 records of file and print 1-7 records
$> head –n -3 file.txt
Range Description
To remove certain lines from a file. Delete line 5 to line 7 from file
sed –i '5,7 d' 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.
Deletes the lines which ends with „os‟ and display the output
cat file1.txt | sed '/os$/d'