Essential Linux Command

Linux Command

*** ls command ***
Option
-a
Lists hidden files also. If a file or directory name starts with a dot, it is referred to as hidden.

-F
Displays file types. Shows / for directories, * for executable files, @ for symbolic links and nothing for text files.

-lh
Displays long listing with file sizes in human readable format.

-l
Displays long listing with detailed file information including file type, permissions, link count, owner, group, size, date and time of last modification, and name of the file.

-ld
Displays long listing of the specified directory, but hides its contents.

-R
Lists contents of the specified directory and all its sub-directories (recursive listing).

-lt
Lists all files sorted by date and time with the newest file first.

-ltr
Lists all files sorted by date and time with the oldest file first.

To list files in the current directory with detailed information, use one of the following:
# ll
# ls -l

*** pwd Command ***
The pwd (present working directory) command displays a user’s current location in the directory tree.

*** cd Command ***
The cd (change directory) command is used to navigate the directory tree.

To go back to the home directory, issue either of the following two commands:
$ cd
$ cd ~

To go to the root directory, use the forward slash character:
$ cd /

*** tty Command ***
This command displays the pseudo terminal where you are currently logged in:
$ tty
/dev/pts/1

or you may also type this command to know who you are:
$ who am i
or
$ whoami

*** whoami Command ***
The whoami (who am i) command displays the username of the user that executes this command.

*** logname Command ***
The logname (login name) command shows the name of the real user that logs in initially. If that user uses the su command to switch identity, the logname command, unlike the whoami command, still shows the real username.

*** id Command ***
The id (identification) command displays a user’s UID (user identification), username, GID (group identification), group name, all secondary groups that the user is a member of, and SELinux security context:
$ id
uid=500(admin) gid=500(admin) groups=500(admin) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

*** groups Command ***
The groups command lists all groups that a user is a member of:
$ groups
admin

*** last Command ***
The last command reports the history of successful user login attempts and system reboots by reading the /var/log/wtmp file. This file keeps a record of all login and logout activities including login time, duration a user stayed logged in, and tty where the user session took place.

To list only system reboot details:
# last reboot

There is another command that lists more detailed information on recent logins and reboots. This command is utmpdump and is executed the following way:
# utmpdump /var/log/wtmp

*** uname Command ***
The uname command produces basic information about the system. Without any options, this command displays the operating system name only. You can use the -a option to get more information.
# uname
Linux
# uname -a
Linux localhost.localdomain 2.6.32-358.el6.i686 #1 SMP Thu Feb 21 21:50:49 UTC 2013 i686 i686 i386 GNU/Linux

*** hostname Command ***
The hostname command displays the system name:
# hostname
localhost.localdomain

*** date Command ***
The date command displays current system date and time.

*** hwclock Command
The hwclock command displays the date and time based on the hardware clock.

*** cal Command
The cal (calendar) command displays the calendar for the current month

*** which Command
The which command shows the absolute path of the command that will execute if run without using the absolute path
# which date
/bin/date

*** whereis Command
The whereis command displays the binary name and the full pathname of the command along with the location of its manual pages:
$ whereis date
date: /bin/date /usr/share/man/man1p/date.1p.gz /usr/share/man/man1/date.1.gz

*** wc Command
The wc (word count) command displays number of lines, words, and characters (or bytes) contained in a text file or input supplied.
# Option
-l
Prints line count.

-w
Prints word count.

-c
Prints byte count.

-m
Prints character count.

*** wall Command
The wall command is used to broadcast a message to all logged in users on the system.

*** ARCHIVING TOOL ***

*** USING tar ***
Command option when using tar command:
-c
Creates a tarball.

-f
Specifies a tarball name.

-j
Compresses a tarball with bzip2 command.

-r
Appends files to the end of an existing tarball. Does not append to compressed tarballs.

-t
Lists contents of a tarball.

-u
Appends files to the end of an existing tarball if the specified files are newer. Does not append to compressed tarballs.

-v
Verbose mode.

-x
Extracts from a tarball.

-z
Compresses a tarball with gzip command.

To create a tarball called /tmp/home.tar of the /home directory:
# tar cvf /tmp/home.tar /home

To create a tarball called /tmp/files.tar containing multiple files located in the /etc directory:
# tar cvf /tmp/files.tar /etc/host.conf /etc/ntp.conf /etc/yum.conf

To append files located in the /etc/xinetd.d directory to home.tar:
# tar rvf /tmp/home.tar /etc/xinetd.d

To list the contents of home.tar:
# tar tvf /tmp/home.tar

To extract files from files.tar:
# tar xvf /tmp/files.tar

To create a tarball called /tmp/home.tar.gz of the /home directory and compress it with gzip:
# tar cvzf /tmp/home.tar.gz /home

To create a tarball called /tmp/home.tar.bz of the /home directory and compress it with bzip2:
# tar cvjf /tmp/home.tar.bz /home

*** Compression Tools ***

*** Using zip and unzip
The following example compresses three files into one called /tmp/files.zip:
# zip /tmp/files.zip /etc/host.conf /etc/ntp.conf /etc/yum.conf

To uncompress the three files, issue the unzip command:
# unzip /tmp/files.zip

*********************************
*********************************
FILE and DIRECTORY OPERATIONS

# The lost+found Directory (/lost+found)
This directory holds files that become orphan after a system crash. An orphan file is a file that has lost its name.

# Creating Files Using the touch Command

#Creating Files Using the cat Command
The cat command allows you to create short text files:
$ cat > newfile

$ ll
total 12
-rw-rw-r–. 1 aghori aghori 48 Jul 28 07:21 file1
-rw-rw-r–. 1 aghori aghori 0 Jul 28 07:30 newfile
drwxrwxr-x. 3 aghori aghori 4096 Jul 28 07:28 scripts.dir1
drwxrwxr-x. 3 aghori aghori 4096 Jul 28 07:30 scripts.dir2
Column 1: The 1st character tells the type of file. The next 9 characters indicate permissions. File permissions are explained at length in Chapter 04 “File Permissions, Text Editors, Text Processors & The Shell”.

Column 2: Displays the number of links that the file or the directory has.

Column 3: Shows the owner name of the file or directory.

Column 4: Displays the group name that the owner of the file or directory belongs to.

Column 5: Shows the file size in bytes. For directories, this number reflects the number of blocks being used by the directory to hold information about its contents.

Columns 6, 7, and 8: Display the month, day of the month, and the time at which the file or directory was created or last accessed/modified.

Column 9: Shows the name of the file or directory.

*** File and Directory Control Attributes
Attribute

Effect on File or Directory

a (append)

File can only be appended.

A

Prevents updating access time.

c (compressed)

File is automatically compressed on the disk.

d (dump)

File cannot be backed up with the dump command.

D
Changes on a directory are written synchronously to the disk.

e (extent format)
File uses extents for mapping the blocks on disk. Set on all files by default.

i (immutable)
File cannot be changed, renamed, or deleted.

j (journaling)
File has its data written to the journal before being written to the file itself. See Chapter 10 “File Systems & Swap” for details on journaling.

S (synchronous)
Changes in a file are written synchronously to the disk.

To list current attributes on install.log:

# lsattr install.log
————-e- install.log

# chattr +i install.log
# lsattr install.log
—-i——–e- install.log

Now, try deleting this file as root:
# rm install.log
rm: remove regular file `install.log’? y
rm: cannot remove ‘install.log’: Operation not permitted

To allow only the append operation on the file:
# chattr +a install.log
# cat /etc/fstab >> install.log

To unset both attributes:
# chattr -ia install.log

*** diff

Compare between two files or two directories

$ diff -c file1 file2
$ diff -c folder1 folder2

Copy folder to a folder
$ cp -r folder2 folder1

*** find Command
The find command recursively searches the directory tree, finds files that match the specified criteria, and optionally performs an action. This powerful tool can be customized to look for files in a number of ways. The search criteria may include searching for files by name, size, ownership, group membership, last access or modification time, permissions, file type, and inode number.

To find files in /home with ownership set to aghori and group membership set to any group but aghori:
# find /home -user aghori -not -group aghori

To find files in the /etc/rc.d directory that were modified more than 120 days ago:
# find /etc/rc.d -mtime +120

To find files in the /etc/rc.d directory that have not been accessed in the last 90 days:
# find /etc/rc.d -atime -90

To find files in the /etc/rc.d directory that were modified exactly 10 days ago:
# find /etc/rc.d -mtime 10

To search for core files in the entire directory tree and delete them as found without prompting for confirmation:
# find / -name core -exec rm {} \;

*** Soft Link
A soft link (a.k.a. a symbolic link or a symlink) makes it possible to associate one file with another. It is similar to a shortcut in MS Windows where the actual file is resident somewhere in the directory structure but you may have multiple shortcuts or pointers with different names pointing to it.
$ ln -s file1 newfile_softlink
$ ll -i
36 -rw-rw-r–. 1 aghori aghori 0 Jul 28 07:30 file1
32 lrwxrwxrwx. 1 aghori aghori 7 Jul 28 14:23 newfile_softlink -> file1

If you remove the original file (file1 in this example), the link newfile_softlink will stay but points to something that does not exist.

*** Hard Link
A hard link associates two or more files with a single inode number. This allows the files to have identical permissions, ownership, time stamp, and file contents. Changes made to any of the files are reflected on the other linked files. All files actually contain identical data.
$ln newfile2 newfile20

After creating the link, run ll with the -i option:

$ ll -i
32 -rw-rw-r–. 2 aghori aghori 0 Jul 28 14:29 newfile2
32 -rw-rw-r–. 2 aghori aghori 0 Jul 28 14:29 newfile20

*** Changing Access Permissions
Add the execute permission for the owner and run the ll command to verify:
$ chmod u+x file1

Add the write permission for group members and public and run the ll command to verify:
$ chmod go+w file1

Add the execute permission for the file owner and run the ll command to verify:
$ chmod 544 file2

Add the write permission for the owner and run the ll command to verify:
$ chmod 744 file2

*** File Ownership and Group Membership
To change ownership from user1 to user2:
# chown user2 file10
# ll file10
-rw-r—–. 1 user2 user1 0 Jul 29 07:00 file10

To change group membership from user1 to user2:
# chgrp user2 file10
# ll file10
-rw-r—–. 1 501 user2 0 Jul 29 07:00 file10

To modify both ownership and group membership in one go:
# chown user1:user1 file10
# ll file10
-rw-r—–. 1 user1 user1 0 Jul 29 07:00 file10

To modify recursively (-R option) all files and sub-directories under dir1 to be owned by user2 and group user2:
# chown -R user2:user2 dir1

*** Executing a Command Immune to Hangup Signals
For example, to copy /sdata1 directory recursively containing 20GB of data to /sdata2, issue the cp command as follows:
# nohup cp -av /sdata1 /sdata2 &

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: