Thursday, 10 April 2014

Basic - File Opetaions

pwd

The pwd is an acronym for Print Working Directory. The pwd command is considered as one of the most frequently used commands on Linux, UNIX like operating systems along with the ls, and cd commands.

Purpose for typing pwd command are,
  - Find the full path to the current directory.
  - Store the full path to the current directory in the shell variable.
  - Verify the absolute path.
  - Verify the physical path i.e exclude symbolic links.

# pwd
/home/subrat

ls

The ls command is one of the most frequently used command in Linux. It lists the directories and files present inside a directory.

- List the files & directories in bare format.
# ls

- Long listing of files & directories.
# ls -l
total 48
-rwxr-xr--. 1 root   root   15504 Apr  2 04:52 iostat.txt
-rw-rw-r-x. 1 subrat subrat 20480 Apr  4 03:45 mon.tar
drwxr-x--x. 2 subrat subrat  4096 Apr  4 07:22 oracle
drwxr-x--x. 2 root   root    4096 Apr  8 08:04 java
-rw-------. 1 root   root     592 Apr  2 04:51 sar.txt
lrwxrwxrwx. 1 root   root      25 Dec 23 21:31 al -> /etc/alternatives

The First character specifies the type of the file as specified below,

-  normal file
d  directory
s  socket file
l  link file

Then rest of the fields are described as below,

1st Field - File Permissions.Each 3 characters refers to the read, write, execute permissions for user, group and others.
2nd Field - Link Count. Number of directory present in a directory. For file the Link count is 1 & for directory , the link count is 2. For sub-directories, the link count is 1.
3rd Field - The owner of the file.
4th Field - The group of the file.
5th Field - Size of the file.
6th Field - Month of last modification.
7th Field - Date of last modification.
8th Field - Time of last modification.
9th Field - File Name.

- Listing all files including hidden files.
# ls -la

- Listing the files with their size.
# ls -ls

- Sort the files by their size.
# ls -lS

- Display files with inode number.
# ls -li

- List files with human readable format.
# ls -lh

- List files in reverse order.
# ls -lr

- List the files according to last modified Time.
# ls -lt

- Display File UID and GID.
# ls -n

- Visual Classification of Files With Special Characters.
# ls -F

Note for the following representations,

    / – directory.
    nothing – normal file.
    @ – link file.
    * – Executable file

- Display Files Recursively.
# ls -R

- List the contents of your home directory.
# ls ~

- List the contents of your root directory.
# ls /

- List the contents of the parent directory.
# ls ../

- List the contents of all sub-directories.
# ls */

- Display a list of directories in the current directory.
 # ls -d */

- Display the information of a specific file.
 # ls -l /path/to/file

- Display the information of a specific directory.
# ls -ld /path/to/dir

- Visual Classification of Files With Colors.
# ls --color=auto

ll

The ll command is the equivalent of 'ls -l'. This command is used to get detail information about files and directories in a directory.

# ll
total 140
-rw-------. 1 subrat subrat 73740 Apr  9 06:42 file1
-rw-------. 1 subrat subrat  3324 Apr  9 06:43 file2
-rw-------. 1 root   root   15504 Apr  2 04:52 iostat.txt
-rw-------. 1 subrat subrat 20480 Apr  4 03:45 mon.tar
-rw-------. 1 subrat subrat    56 Apr  9 05:47 my.pkt
-rw-------. 1 root   root     549 Apr 11 08:40 out.mp

Just like the different options of ls can be used for ll too. Try more options.

dir

The dir command is used to list all the directories and files present in a directory. It does the same operation like ls command. It does not support content highlighting. For more info visit man page for dir.

# dir
dir1 dir2 file.txt

mkdir

The mkdir command is used to create directory or directories.

- Creating Directory/Directories  in current working directory.
# mkdir Dir1
# mkdir Dir2 Dir3

- Creating Directory/Directories in a specified path.
# mkdir /home/subrat/Dir1
# mkdir /home/subrat/Dir2 /root/Dir3

- Creating nested directories.
# mkdir -p /opt/apps/oracle/data

The above command will first create opt, then apps, then oracle & finally data.

cd

- Change working directory from present working directory using relative path.
# cd subrat/

Note: Your relative path starts from your present working directory.

- Change working directory from present working directory to any location.
# cd /path/to/directory

Note: In this case you are starting from / .So, you are changing the directory using the absolute path.

- Change working directory to user's home directory.
# cd ~
# cd

- Change directory to previous working directory which we changed from.
# cd -

- Change working directory to parent directory.
# cd ..

- Changing working directory three levels up in the directory.
# cd ../../..

cp

The cp command is used to copy one or more files or directories from source to destination. The original file remains unchanged, and the new file may have the same or a different name.

Syntax: cp [option] source destination

Note: While copying make sure that your destination location should not contain the file or directory with the same name as your source file or directory. Else the new content will be overwritten.

- To make a copy of a file in the current directory as a new file.
# cp file.txt new-file.txt

- To copy a file from your current directory into another directory.

# cp file_name /path/of/new_file

- To copy a directory from one source to another.
# cp -r dir1 /path/of/new_dir

- To copy multiple files into a directory.
# cp file2 file3 file4 dir1

- To copy multiple directories.
# cp -r src/ bin/ /home/subrat/

- To explain what is being done while copying. i.e. verbose
# cp -v file1 /path/to/new_file

- To copy a file or directory and preserve the attributes like modification date, time, and access control list associated with the source file or directory.
# cp -p file_name /path/to/new/location/myfile
# cp -rp dir_name /path/to/new/location/mydir


- To copy all the files in a directory to a new directory.
# cp * /home/subrat/backup

- To copy a directory, including all its files and sub-directories, to another directory.
# cp -R dir1/* /home/subrat/backup

- To ask for Confirmation before overwriting (interactive mode)if a file exist with the same name in destination.
# cp -i test.c bak
cp: overwrite 'bak/test.c'? y

- To take a backup before copying into a destination.
# cp -b file.txt  /home/subrat/
# cd /home/subrat/
# ls -l
-rw-r--r-- 1 subrat unix 1020 Apr  8 13:36 file.txt
-rw-r--r-- 1 subrat unix 1038 Apr  8 13:15 file.txt~

- To copy a write-protected file forcefully.
# cp -f file.txt /path/to/file.txt

- To copy some files having a common pattern by using wild-cards as shown below. In this example, all txt extension files gets copied.
# cp dir1/*.txt /home/subrat/text/

mv

The mv command is used to move files and directories from one location to another.
Apart from moving the files, it can also rename a file or directory.

Syntax: mv oldname newname

- To rename a file.
# mv file file_new

- To move a file from source to destination.
# mv filename /path/to/destination

- To move a file from source to destination with a new name.
# mv filename /path/to/destination/new_filename

- To rename a directory.
# mv -r dir1 dir2

- To move a directory from source to destination.
# mv -r dir1 /path/to/destination

- To move a directory from source to destination with a new name.
# mv -r dir_old /path/to/destination/dir_new

- To take a backup of destination before Overwriting.
# mv --suffix=.bak file_name /path/to/destination/

The above command creates another file with .bak which holds the old data.

- To move a write-protected file forcefully.
# mv -f file /path/to/destination

- To move a file interactively.
# mv -i sample.txt /backup

It will Prompt for a Confirmation Before Overwriting.
# mv -i sample.txt sample1.txt
mv: overwrite 'sample1.txt'? y

- To move some files having a common pattern by using wild-cards as shown below.
# mv *.txt /home/subrat/

rmdir

The rmdir command is used to delete an empty directory or set of empty directories.

- To delete an empty directory.
# rmdir dir_empty
# rmdir /path/to/empty_dir

- To delete mutltiple empty directories.
# rmdir dir1 dir2  dir3

- To delete nested empty directories.
# rmdir -p dir1/dir2/dir3

rm

The rm command is used to remove files or directories. Remove your files wisely! The effects of an rm cannot be undone.

- To remove file/files.
# rm file1
# rm file1 file2
# rm /path/to/file/

- To remove directory/directories.
# rm -r dir

- To remove a write-protected file or directory forcefully.
# rm -f file

- To prompt the user for confirmation before removing each file and directory.
# rm -i file

- To remove some files having a common pattern by using wild-cards as shown below.
# rm -f *.txt

No comments:

Post a Comment