Pages

Thursday, 28 August 2014

File & Directory Command

Command Description
touch Creates the empty file(s) if it does not exist,
Otherwise change the timestamp of file
cat 1.creates the file and save the content
2.Display the file contents
3.Append the content in existing file
file Displays wether its file or directory
more Pagewise display with %
less Pagewise display without %
mkdir Creates the new directory
cd To change the directory
rm To remove or delete the files / directories
rmdir To remove a empty directory
ls To list all files and directories of target directory
cp Copy file(s) from source to destination
mv Move the file(s) from source to destination
ln To make hard or soft link netween the files

touch command
Used to create the empty file ,it is also used to change the timestamp of existing files
Syntax
$ touch [option] file_name(s)

Examples
To create multiple empty file at a time ,type below command
$ touch file1 file2 file3 file4

To change both the access and modification times to the current time of existing file file1
$ touch -am file1

To use the times of file1 for file2, use oprion -r(reference) as below
$ touch -r file1 file2

The -d and -t options allow the user to add a specific last access time, to change the last access time of file1 to 10 Mar 2014 09:30 would be enclosed in single quotes
and used as follows
$ touch -d '1 Mar 2014 09:30' file1

cat command
Cat can be used for the following purpose
1.To reading or display the content of file
2.New file creation
Syntax
$ cat [option] [filename]

Examples
To reading or display content of file1 ,write below command
$ cat file1

To display the content of more than one files
$ cat file.txt abc.txt f1.txt

The above command will display the content of files in same order ,first it will show the content of file.txt then below that abc.txt then in last f1.txt

To create a new file and writing some content in file.
$ cat > file1.txt
  ----
  ----
 ctr+d (to save all content in file1.txt)

Note : if file1.txt already exist then new data will get override.

To append the data in existing file
$ cat >> file1.txt
  ----
  ----
 write some data and hit 'ctr+d' to save the file1.txt

file command
To check type of file ie: whether it is file or directory.
Syntax
$ file [option] [file/dirname]

more command
To display page wise ,useful when file having more pages which can not be display at a time on the computer screen.
The more command allow you to reading the content from page1
$ more file1.txt

less command
Similar to more command.
This command allows you to view a text file without fear of accidently modifying it, as you would with a text editor
$ less file1.txt

mkdir command
Creates the directory in the current directory, if they do not already exist.
Syntax
$ mkdir [option] [dirname]

Examples
To create a directory dir1 in current directory
$ mkdir dir1

To create a dir in a path ,suppose i would like to create a directory dir2 inside another directory name 'freelancer'
Then we need to give the path of the directory 'freelancer' followed by new directory 'dir2' as follow
$ mkdir /home/mukesh.kumar/Desktop/freelancer/dir2
       (or)
--in general

$ mkdir path/dir2

To create the hierarchy of directories ,use -p(parents) option
$ mkdir –p dir1/dir2/dir3

cd command
To change the shell working directory
Following command will change current directory to dirname if dirname is present in current directory
$ cd dirname

To change the directory in the mentioned path
$ cd path/dir1

To changes to the home directory
$ cd (or) $ cd ~

To changes to the root directory (i.e. />)
$ cd /

To changes to the Parent directory
$ cd ..

rm command 
Used to delete the file or directory.
Syntax
rm [option] [-r directories] [filename]

Examples
To remove multiple files
$ rm file1 file2 file3 [file4..]

To remove a directory recursively
$ rm -r dirname

To remove file interactively
$ rm -i filename.txt

rm: remove regular file `filename.txt'?

Note : if rm command used by using '-i' option it will always ask user to confirm by displaying a message.

Removing forcefully
Assume filename is not having write permission for the user. If the user is giving the below command
$ rm filename

It will display confirmation message before removing the file.
But if user is giving the command with ‘-f’ option as follows:
$ rm –f file_name

It will remove the file forcefully without displaying confirmation message

To remove a file whose name starts with a `-', for example `-foo',
use one of these commands:
$ rm -- -foo
   (or)
$ rm ./-foo

rmdir command
rmdir can be use to remove the empty directory
$ rmdir emptydir

ls (list) command
List information about the FILEs (the current directory by default).
Syntax
$ ls [option] [file]

Note : To check the various option ,use the following command
$ ls --help

Examples
To list all file and directories of current directory
$ ls

To list files and directories of target directory in long format
(includes accesspermissions, size of file, last modification date & time, no of links etc.)
$ ls -l

To list files and directories of target directory
$ ls dirname

To list all the files and directories (including hidden) of target directory
$ ls -a dirname

To list files and directories of target directory in reverse order.
$ ls -r dirname

Note : by default it shows in alphabetically.
To list files and directories of target directory with their inode value.
$ ls -i dirname

To list all the files and directories of target directory recursively
$ ls -R dirname

To list files and directories of target directory with information of format of file
(ie : directory name with ‘/’ as suffix, executable files with ‘*’ as suffix etc.)
$ ls -F dirname

To list with inode value
$ ls -i dirname

To list all the files and directories(including hidden files) of target directory in long format
$ ls -al dirname

To list files and directories with inode values recursively of target directory in long format
$ ls -iRl dirname

To list files and directories with last modified in reverse order
$ ls -ltr dirname

cp command 
To copy files and directories from source to destination.
Synatx
$ cp [option] source destination

Examples
To create copy of file1.txt with name file2.txt in same directory
$ cp file1.txt file2.txt

To create copy of multiple source file(s) with same name in source directory
$ cp file1.txt [file2.txt] dirname

To copy dir1 or content of dir1 in dir2
$ cp -r dir1 dir2

Note : if dir2 is not present : create dir2 in current dir,copy content of dir1 in dir2
            if dir2 present : COPY sub tree structure rooted at dir1 in dir2
           ( inside dir2 we will get dir1)

To copy files interactively. It will ask for confirmation by displaying a message to the user but only in the case of overwriting.
$ cp –i source destination

mv command
Used to rename source file/directory to destination name
Move source file/directory to destination directory
Syntax
$ mv [option] source destination

To rename source file with destination name
$ mv file1.txt file2.txt

To move source files in destination directory
$ mv file1.txt [file2.txt] dirname

To move dir1 or content of dir1 into dir2
$ mv -r dir1 dir2

Note : if dir2 is not present :rename dir1 with name dir2
           if dir2 present then move sub tree structure rooted at dir1 in dir2
To Move or rename interactively. It will ask for confirmation by displaying a message to the user but only in the case of overwriting.

$ mv –i source destination

ln( link ) command 
To create hard and soft link between the files
There are two types of link we can create

1.Hard Link :
While creating a link from source to Destination file.The changes done in the source file will be effected in destination file.
But when The source file is deleted the destination file remains the same.

2.Soft Link :
While creating a link using soft link from source to destination file → the changes done
in the source file will be effected in Destination file too.
But when the source file is deleted the destination file also gets invisible. i.e when u tried to check the destination file its says no such file or directory

To make hard link between the file
$ ln sourcefile destinationfile

To makes soft(or symbolic) link between files
$ ln -s source destination


Note : we can not create hard link for directory




No comments:

Post a Comment