Linux | Cloud | DevOps | Scripting

Breaking

Tuesday, 2 September 2014

Basic Linux (Red Hat) Commands


The 'cat' command:

We use cat command to create a file with content and to display or modify the contents of a file.

Syntax:

# cat > filename (press enter)
Enter the text you want to put inside the file (press enter)
Control D (to save the file)

Example:
# cat > testfile
This is my test file.
^d

To display the contents of file:
# cat testfile
This is my test file.

To append the data in the already existing file:
# cat >> testfile
I created this for testing purpose.
^d

# cat testfile
This is my test file.
I created this for testing purpose.

The 'touch' command:

By touch command we can create multiple files at the same time.
# touch file{1..3}.txt

Display the files created by touch command:
# ls
file1.txt
file2.txt
file3.txt

The 'mkdir' command:

By using mkdir command we can create a new directory.
# mkdir dir_name
# mkdir dir1
# ls
dir1

Making multiple directories inside a directory:
# mkdir -p dir1/{dir1.1,dir1.2,dir1.3}
# ls
dir1.1  dir1.2  dir1.3

The 'cp' command:

The 'cp' command is used to copy the files from one location to another location.
# cp <source_file> <destination_file>
# cp testfile /tmp/testfile

Copy directories from one location to another location:
# cp -rvfp <dir_name> <destination_name>
# cp -irvf testfile testfile2

The 'mv' command:

We use 'mv' command to move a file from one location to another location. This act is just like cut and paste.
# mv <file_name> <destination_directory>
# mv testfile dir1

Same case in directory:
# mv <directory_name> <destination_location>
# mv dir1 dir2

Renaming a file:
# mv <old_name> <new_name>
# mv testfile mytestfile

Renaming a directory:
# mv <old_name> <new_name>
# mv dir1 dir2

The 'rm' command:

The 'rm' command is used to remove files and directories.
Removing a file:
# rm <filename>

Removing a directory:
# rm –rf <dirname>
Here, r stands for recursive and f stands for forcefully.

Filter commands:

The commands which are used to filter the output are:
1. less
2. more
3. head
4. tail
5. sort
6. cut
7. sed

The 'less/more' command: 

The less and more commands are same and used to see the output line wise or page wise.
e.g. # less /etc/passwd

or

# more /etc/passwd

Press enter to scroll line by line.
Press d to go to the next page.
Press b to go to the previous page.
Press / to search a word in the file.
Press v to go to vi mode to make any change in to the file.

The 'head' command: 

The 'head' command is used to display the top 10 lines of a file.

e.g. # head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

If we want to see only 5 top lines of this files, we need to use:

# head -5 /etc/passwd

The 'tail' command: 

The 'tail' command is used to display the last 10 lines of a file:

e.g. # tail /etc/passwd
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
divakar:x:1000:1000:divakar rastogi:/home/divakar:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
neha:x:1001:1001::/home/neha:/bin/bash
u1:x:1002:1002::/home/u1:/bin/bash
u2:x:1003:1003::/home/u2:/bin/bash

In this case also, we can use -n to get desired number of lines as output (where n can be any number).

The 'sort' command: 

The 'sort' command is used to get output in numeric or alphabetic order.
# sort <filename>

To remove the duplicate entries in output we can use:
# sort -u <filename>

The 'cut' command:

The 'cut' command is used to pick the given expression (in columns).
# cut -d: -f<column_number> <filename>

e.g.:
# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail

The 'sed' command:

The 'sed' command is used to search a word in the file and replace that with the desired word. This command only make changes in output, the original file remains the same.’
e.g.:
# cat test
My file name is test.
# sed 's/test/testfile/g' test
My file name is testfile.

I/O Redirection:

We use redirection to copy the output of any command or file into a new file. There are two ways (> and >>) by which we can use redirection.

Syntax:
# command > new file

e.g.:
# cat testfile >> file1.txt

Find command:

We use find command to find files or directories path.

Syntax:
# find / (under root) -options filename

We can use below-mentioned options with find command:
-name: for searching a file with its name
-type: for searching a particular type of file
-inum: for searching a file with particular inode number
-user: for files whose owner is a particular user
-group: for files belonging to particular group

pwd: To display the current location.

cd: To change the directory.

mkfs: To provide filesystem OR to format

w: Who is login on which time and what they are working

who: To display the information about all currently logged In users

whoami: Tells us by which user we are login

logname: To display the real user who logged in initially

id: To displlay a user’s UID (User Identification), username, GID (Group identification), groupname
and all secondary groups that the user is a member

groups: To list all groups that a user is member of

uname: To display basic information about the system

hostname: To display the system name

clear: To clear the screen

date: To display current system date and time

cal: To display the calander

ping: To test connectivity

get: Retrieves one file

mget: Retrieves multiple files

put: Upload one file from your computer to remote host

mput: Upload many files from your computer to remote host

which: To show the absolute path of the command

whereis: To display the binary name and full path

mkdir: To create a directory

rmdir: To remove a directory

touch: To create a file

rm: To remove a file

useradd: To add a user

userdel: To delete a user

groupadd: To add a group

groupdel: To delet a group

chmod: To change the permission of a file or directory

crontab: To change cron jobs

palimpsest: To view full disk use in graphic mode

Create a user with UID 5001:
# useradd -u 5001 username          //if we want to create a new user
# usermod -u 5001 username          //if we want to make modification in existing user




4 comments:

  1. thanks for uploading this article , it's very helpful for us.

    ReplyDelete
  2. superb, Please add shell scripting KB

    ReplyDelete
  3. rmdir works only when directory is empty..
    to remove the directory with content, below is one of the command: rm -rf {dir-name}

    Good stuff provided in the blog.. thanks for the hard work...
    :):)

    ReplyDelete
  4. Above unknown is Puneet Nandanwar :)

    ReplyDelete

Pages