Basic Linux Commands

List of Basic Linux Commands

Create directory

Mkdir is the command which we use to create directory, mkdir is short form of make directory. The command structure is as per below.

mkdir <directory_name>

Example

mkdir ivlsi_physicalDesign

Change Directory

Change directory is command which helps us to switch one directory to another. cd is the short form of change directory. The command structure is as per below.

 cd <directory_name>

Example

gt; cd .. ##This command takes you to one directory back.
gt; cd /xyz/physical_design/timing #This command will take you to timing directory.
gt; cd #If you are using only cd command then this will take you to home directory.
gt; cd - ##This command will take you to previous directory you were in.

Copy Files and directories

To copy file or directory we use the command “cp”. cp is short form of copy. The command structure is as per below.

cp <file name/ directory name to be copied> <location to be copied>

Example

gt; cp file1.tcl ../ ##This command will copy file1.tcl into previous directory.
gt; cp file1.tcl file2.tcl ##This command will copy all the content of file1.tcl into file2.tcl
gt; cp * ../ ##This command will copy all the files/directories present will be copied in one directory up.
gt; cp -i file1.tcl file2.tcl
cp: overwrite `file2.tcl'?
##This command will ask for if you want to overwrite, type y then only it will overwrite.
gt; cp -f file1.tcl file2.tcl ##If user permission is not there and still wanting to copy then use -f option.
gt; cp -b file1.tcl file2.tcl ##This will create a backup file along with the regular file and below three files will get created.
file1.tcl file2.tcl file2.tcl~
gt; cp -rf file1 .. ##This command will copy the file1 directory to one directory up. While copying directory we must need to use “-rf” command.

Move files and directory

To move a file or directory we need to use the command “mv”. Using cp we will not delete the file/directory which we are copying but if we use mv then we are deleting it.

gt; mv file1.tcl .. ##This command will move the file1.tcl to on directory up, means file1.tcl will not be there in present directory.
gt; mv file1.tcl file2.tcl ##This command will move the content of file1.tcl to file2.tcl and delete the file1.tcl at the same time.
gt; mv -f file1.tcl .. ##This file will forcefully move the file1.tcl even if permission is denied.
gt; mv -b file1.tcl file2.tcl ##This command will create a backup file along with destination file. Content will be file2.tcl file2.tcl~.
gt; mv -n file1.tcl file2.tcl ##This command ensure that file will not be overwritten.

Remove files and directories

To remove/delete a file or directory we use rm/rm -rf command.

rm <fileName>
rm -rf <fileName>

Example

gt; rm file1.tcl ##This command removes file1.tcl.
gt; rm file1.tcl file2.tcl ##This command removes all the files written after rm.
gt; rm -rf file1 ##This command will delete the directory file1.

Listing

Listing all the files present in the directory using command “ls”. Let’s look into few commands.

gt; ls ##This command list all the files and directories present in present working directory.
gt; ls -lrt ##This command list all the files along with permissions, username, group permission, file number months and Time, file name.
-rw-r----- 1 ivlsi pd_grp 76808 Nov 2 05:25 file1.tcl
gt; ls -R ##This will list all the files in the sub-directories as well.
gt; ls -a ##This will show all the hidden files present in the directory. Sometimes the files start with dot (.) are hidden in the
gt; ls -al will list the files and directories with detailed information like the permissions, size, owner, etc.
gt; ls -s ##This command will list file size
gt; ls -S ##This command will short files by its size
gt; ls -r ##This command will list in reverse order
gt; ls -R ##This command list recursively directory tree.
gt; ls -t ##This command shorts files by date and time

pwd

Present working directory, this command says that currently which directory you are into.

Echo

Echo command is used to display text or argument, passed in argument.

Touch command

There are few times we just need a file with 0KB (Blank file). We can create an empty file in gvim as well.

gt; gvim file1

In the empty file do, :wq!

Or we directly touch the file

gt; touch file #It is used mostly when a run is over, and we need to touch the PASS file,

CAT command

CAT is short from of concatenate which helps to display the content of file in terminal or merge 2 or more files. Let’s see the below examples.

#1. Content of file1.tcl is as per below.
abc
123
def
456

$> cat file1.tcl
abc
123
def
456

#2. Suppose we need to merge two files; we can do this by using cat commands. Now cat command will paste first file then second file. If there are more files then it will follow the same same sequence as it will paste first file then second file then third file then fourth and so on.
File1.tcl File2.tcl
abc cba
123 321
def fed
456 654

$> cat File1.tcl File2.tcl
abc
123
def
456
cba
321
fed
654

Note: If one wants to redirect this in one file then use greater than (>). Let’s see the example below.

$> cat File1.tcl File2.tcl > File3.tcl

Once you use the above command, the File3.tcl content is as per below.
abc
123
def
456
cba
321
fed
654

#3. If you want to print the content of file with the line numbers, then one can use below command.
$> cat File1.tcl

1 abc
2 123
3 def
4 456

#4. If you don’t want to open VIM and write into a file using CAT then use below method.

$> cat <The content of file which one wants to write> > file1.tcl

For EG.
$> cat abc 123 def 456 > file1.tcl

The content of the file1.tcl is abc 123 def 456.

Special case

Suppose we want to cat two line, where lines should copy parallelly, we use Paste command
File1.tcl File2.tcl
abc cba
123 321
def fed
456 654
$> paste File1.tcl File2.tcl
abc cba
123 321
def fed
456 654

Absolute path or real path

If one wants to find the absolute path of a file, then use the command “abspath” or “realpath” with the file.
abspath file1.tcl -> This will print the full path of the file1.tcl
realpath file1.tcl -> This will print the full path of the file1.tcl

Disk Utility Command

#1. df -h ## To check the memory of the total disk area used and available. If you run this command you can get below information’s like Filesystem Size Used Avalable Use% and mounted on.

Filesystem Size Used Avail Use% Mounted on

If wants to check this for current directory then use dot (.) command along with df -h,

#2. If you want to see the directories/files how much memory is getting consumed, then use below command. This will write the memory then file/directory name. Where M is megabyte and K is kilobyte.

du -sh *

For Eg.
1.7M File1.tcl
522K File2.tcl

Modes of Files/Directories access

There are different modes of files to handle Read (R), Write (W), Execution (X).

Read (r) : This allows file to read only of the content present.
Write (w) : This allows file to write/edit in the file. If this is a directory, then user can add/delete the files present.
Execution (x) : This allows file to run if this is a script file.

To represent the permission mode of files/directories we use Octal number representation where all the number from 0-7 having different values, which will be used with chmod command in linux.

Octal NumberPermissions
Linux Representation
0No Permission
1Execute–x
2Write-w-
3Execute (1) and Write (2) [1+2=3]-wx
4Readr–
5Read (4) and Execute (1) [4+1=5]r-x
6Read (4) and Write (2) [4+2=6]rw-
7All Permission (Read (4), Write (2), Execute (1)) [4+2+1=7]rwx

Let’s understand more with the commands now.

[-][-][-] -> This is the way where we change file permission.

[-] -> The first one is Owner Permissions.
[-] -> The second is Group Permissions.
[-] -> The third one is Other Permissions.

gt; chmod 777 #This gives file permission to read write and execute to Owner, Group and Other permissions.
gt; chmod 775 #This gives file permission to read write and execute in owners and group but not for other permissions.
gt; chmod 755 #This gives read write and execute permission to Owners but not for group and other permissions.
gt; chmod 555 #This gives only read and execute permissions to owner, group and others.
gt; chmod 743 #This gives read write and execute permissions to owner, group and others but read permission to group and write and execute permission to others.
gt; chmod 043 #This gives no permission to owner but read permission to group and write and execute permission to others.

You can change owner of the file using chown command.
chown user filelist
Eg. chown Hari File1.tcl

You can change the group permission of the file using chgrp command.
chgrp -R <Group Name> <Directory Name>
chgrp -R timing_grp timingReports

User details and change

If you want to check the username using the terminal, then use “whoami” command.
If you want to change the user then use “su” command, short form of switch user is “su”.
su <user_name>
eg. su ivlsi_2 ##Now this will look for password of ivlsi_2 user.

If one wants to check the groups, you are part of then use the command “groups”.

Sorting

There can be many lines in a file which is repetitive, to avoid repletion in a file we use the command “sort”.
$> cat file1.tcl
abc
bca
cab
abc

Eg.
gt; sccj013110> sort file1.tcl
abc
abc
bca
cab
gt; sort -u file1.tcl
abc
bca
cba
gt; sort -c file1.tcl
sort: file1.tcl:4: disorder: abc

Jobs information’s and kill:
If you want to see the running jobs, then use the command “jobs”. To kill the jobs one can use “kill” command flowed by job ID.

Admin
Admin

Leave a Reply