Basic’s

To check the present working directory

[root@comp1 ~]# pwd
/root

To see the contents of a directory (folder)
[root@comp1 ~]# ls
Options:
-l Long list including attributes
-a All files and directories including hidden
-d To check for a particular file or directory
-R Recursively, to see the contents in a tree structure

To list the contents of the present working directory
[root@comp1 ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog

To see the list of files and directories along with their attributes (properties)
[root@comp1 ~]# ls -l
total 76
-rw-r–r– 1 root root 1049 Apr 2 18:30 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 Mar 14 20:13 Desktop
-rw-r–r– 1 root root 4687 Apr 2 18.30 install.log
-rw-r–r– 1 root root 5136 Apr 2 18.30 install.log.syslog

To see all files and directories including hidden files of and directories
[root@comp1 ~]# ls -a
. .camel_certs .esd_auth .gnome2_private .metacity
.. .config .evolution .gstreamer-0.8 .mozilla
anaconda-ks.cfg .cshrc .fonts.cache-1 .gtkrc .nautilus
.bash_history Desktop .gconf .gtkrc-1.2-gnome2 .recently-used
.bash_logout .dmrc .gconfd .ICEauthority .rhn-applet.conf
.bash_profile .eggcups gnome install.log .rnd
.bashrc .elinks .gnome2 install.log.syslog .ssh

To check if a particular fle or directory is present
[root@comp1 ~]# ls -d Desktop
Desktop


To see tree structure of nested directories
[root@comp1 ~]# ls -R /opt
/opt:
example
/opt/example:
examplea
/opt/example/examplea:
exampleb
/opt/example/examplea/exampleb:
examples
/opt/example/examplea/exampleb/examples:

To see a list of all file or directories starting with a particular letter
[root@comp1 ~]# ls i*
install.log install.log.syslog

To see the attributes of a particular file or directory
[root@comp1 ~]# ls -ld install.log.syslog
-rw-r–r– 1 root root 5136 Apr 2 18.30 install.log.syslog


Creating files and adding data using cat command
[root@comp1 ~]# cat

To create a file along with some data
[root@comp1 ~]# cat > salesreport
This file contains the sales report for the month of May.
Ctrl+d

To create a file along with some data
[root@comp1 ~]# cat > mktgreport
This file contains the mktg report for the month of May.
Ctrl+d

To read the contents of a file
[root@comp1 ~]# cat salesreport
This file contains the sales report for the month of May.

To append (add) to a file
[root@comp1 ~]# cat >> mktgreport
This year the efforts in mktg have increased.
Ctrl+d

Check the contents of the file mktgreport
[root@comp1 ~]# cat mktgreport
This file contains the mktg report for the month of May.
This year the efforts in mktg have increased.

To add contents of two files into a third file
[root@comp1 ~]# cat salesreport mktgreport >> completereport


Check the contents of the file mktgreport
[root@comp1 ~]# cat completereport
This file contains the sales report for the month of May.
This file contains the mktg report for the month of May.
This year the efforts in mktg have increased.



Creating 0 byte (empty) files using touch command:
Creating a single file with the touch command
[root@comp1 ~]# touch

Creating a single file with the touch command
[root@comp1 ~]# touch file1

To check if the file has been created
[root@comp1 ~]# ls -ld file1
-rw-r–r– 1 root root 0 Apr 2 18.30 file1

Creating multiple file using the touch command
[root@comp1 ~]# touch file2 file3 file4

To check if the file has been created
[root@comp1 ~]# ls -ld file*
-rw-r–r– 1 root root 0 Apr 2 18.30 file1
-rw-r–r– 1 root root 0 Apr 2 18.30 file2
-rw-r–r– 1 root root 0 Apr 2 18.30 file3
-rw-r–r– 1 root root 0 Apr 2 18.30 file4



To change the timestamp (date and time of modification) of a file or directory
[root@comp1 ~]# touch

To change to the current date and time
[root@comp1 ~]# touch file1

To check if the timestamp has changed
[root@comp1 ~]# ls -ld file1
-rw-r–r– 1 root root 0 Apr 2 19.00 file1
Syntax:-
[root@comp1 ~]# touch -t YYYYMMDDhhmm

To change to a different date and time
[root@comp1 ~]# touch -t 200905150645 file2


To check if the timestamp has changed
[root@comp1 ~]# ls -ld file2
-rw-r–r– 1 root root 0 May 15 06.45 file2



Creating directories
[root@comp1 ~]# mkdir

Creating a single directory
[root@comp1 ~]# mkdir directory1

Creating multiple directories
[root@comp1 ~]# mkdir directory2 directory3 directory4
To check if the directories have been created
[root@comp1 ~]# ls -d directory*
directory1 directory2 directory3 directory4

To create nested directories (sub directories inside directories)
[root@comp1 ~]# mkdir -p d1/d2/d3/d4


To check if the nested directories have been created
[root@comp1 ~]# ls -R d1
d1:
d2
d1/d2:
d3
d1/d2/d3:
d4
d1/d2/d3/d4:

Note:- To list the contents and create a file or directory in a location different from the present working directory, specify the complete path (example /var/directory1)



Directory Navigation
[root@comp1 ~]# cd

To go into a directory
[root@comp1 ~]# cd dir1
[root@comp1 dir1]# cd dir2
[root@comp1 dir2]#

To back one level
[root@comp1 dir2]# cd ..
[root@comp1 dir1]#

[root@comp1 dir1]# cd ../..
[root@comp1 /]#

To go to the previous working directory
[root@comp1 /]# cd –
/root/dir1

To go to current logged in user’s home directory
[root@comp1 dir1]# cd
[root@comp1 ~]# pwd
/root

Copying Files and Directories
[root@comp1 ~]# cp

To copy a file
[root@comp1 ~]# cp completereport /opt

To check if the file has been copied
[root@comp1 ~]# cd /opt
[root@comp1 opt]# ls
completereport

To copy a directory
[root@comp1 ~]# cp -r directory1

To check if the directory has been copied
[root@comp1 ~]# cd /opt
[root@comp1 opt]# ls
completereport directory1

Moving files and directories
[root@comp1 ~]# mv

To move a file
[root@comp1 ~]# mv salesreport /opt

To check if the file has been moved
[root@comp1 ~]# ls
anaconda-ks.cfg dir1 directory3 file2 install.log
completereport directory1 directory4 file3 install.log.syslog
Desktop directory2 file1 file4 mktgreport

[root@comp1]# ls /opt
completereport directory1 salesreport

To move a directory
[root@comp1 ~]# cp -r dir1

To check if the directory has been moved
[root@comp1 ~]# cd /opt
[root@comp1 opt]# ls
completereport directory1 dir1 salesreport

Renaming files and directories
[root@comp1 ~]# mv

To rename a file
[root@comp1 ~]# mv completereport fullreport

To check if the file has been moved
[root@comp1 ~]# ls
anaconda-ks.cfg directory2 file1 file4 install.log.syslog
Desktop directory3 file2 fullreport mktgreport
directory1 directory4 file3 install.log

Deleting Directories & Files
[root@comp1 ~]# rmdir

To delete an empty directory
[root@comp1 ~]# rmdir directory2

To check if the directory has been deleted
[root@comp1 ~]# ls
anaconda-ks.cfg directory3 file2 fullreport mktgreport
Desktop directory4 file3 install.log
directory1 file1 file4 install.log.syslog
Deleting a file or a directory
[root@comp1 ~]# rm
Options:
-r recursive
-f forcefully

To delete a file
[root@comp1 ~]# rm file4
rm: remove regular file `file4′? y

To check if the file has been deleted
[root@comp1 ~]# ls
anaconda-ks.cfg directory3 file2 install.log
Desktop directory4 file3 install.log.syslog
directory1 file1 fullreport mktgreport

Example:- To delete a file forcefully
[root@comp1 ~]# rm -f mktgreport

To check if the file has been deleted
[root@comp1 ~]# ls
anaconda-ks.cfg directory3 file2 install.log
Desktop directory4 file3 install.log.syslog
directory1 file1 fullreport

To delete a directory
[root@comp1 ~]# cd /opt
[root@comp1 opt]# rm -rf dir1

To check if the directory has been deleted
[root@comp1 opt]# ls
completereport directory1 dir1 salesreport

To view the system Date and Time
[root@comp1 ~]# date
Fri 4 Jul 14:35:35 IST 2009

To change the system date and time
[root@comp1 ~]# date –s “mm/dd/yyyy hh:mm:ss”

[root@comp1 ~]# date –s “07/15/2009 00:06:00 “
Tue Jul 15 00:06:00 IST 2009


To view the Calender
To see the current month
[root@comp1 ~]# cal

To see the past, current and next month
[root@comp1 ~]# cal -3

To see the calendar for the month of December of the current year
[root@comp1 ~]# cal 12

To see the calander for the month of December for the year 2010
[root@comp1 ~]# cal 12 2010

To see the calander for the full year
[root@comp1 ~]# cal 2009


To view Help for a Command (manual pages)
[root@comp1 ~]# man

To see the help for the command mkdir
[root@comp1 ~]# man mkdir

To see the content screen wise
[root@comp1 ~]# less /etc/mail/sendmail.mc
dnl #
dnl # This is the sendmail macro config file for m4. If you make changes to
dnl # /etc/mail/sendmail.mc, you will need to regenerate the
dnl # /etc/mail/sendmail.cf file by confirming that the sendmail-cf package is
dnl # installed and then performing a
dnl #
dnl # make -C /etc/mail
dnl #
include(`/usr/share/sendmail-cf/m4/cf.m4′)dnl
VERSIONID(`setup for linux’)dnl
OSTYPE(`linux’)dnl
dnl #
dnl # Do not advertize sendmail version.
:


To veiw the top 10 lines of a file
[root@comp1 ~]# head anaconda-ks.cfg
# Kickstart file automatically generated by anaconda.

install
cdrom
key 49af89414d147589
lang en_US.UTF-8
keyboard us
xconfig –startxonboot
network –device eth0 –bootproto dhcp
rootpw –iscrypted $1$MPNm.Pdp$2eTr4vp5.R9l2XUPhFLHJ1


To veiw the top 5 lines of a file
[root@comp1 ~]# head -5 anaconda-ks.cfg
# Kickstart file automatically generated by anaconda.

install
cdrom
key 49af89414d147589


To veiw the bottom 10 lines of a file
[root@comp1 ~]# tail anaconda-ks.cfg
#part /var –fstype ext3 –size=8000
#part / –fstype ext3 –size=5000
#part swap –size=2048
#part /home –fstype ext3 –size=2000

%packages
@cluster-storage
@clustering
@gnome-desktop
@virtualization


To veiw the bottom 3 lines of a file
[root@comp1 ~]# tail -3 anaconda-ks.cfg
@clustering
@gnome-desktop
@virtualization

Leave a comment