Sunday 13 March 2016

Managing Files and Directories - Linux


                In Linux all are files only. Based on the type we identify the files. In this article we are going to see how to create, list, copy, move and delete the files and directories. 

1. Creating files and Directories
2. Listing Files and Directories
3. Copying Files and Directories
4. Moving files and directories
5. Deleting Files and Directories


Create empty files 

To create an empty files, we have to use touch command

                           touch is a standard Unix command-line interface program which is used to update the access date and / or modification date of a file or directory. In its default usage, it is the equivalent of creating or opening a file and saving it without any change to the file contents. Touch eliminates the unnecessary steps of opening the file, saving the file, and closing the file again. Instead it simply updates the dates associated with the file or directory. An updated access or modification date can be important for a variety of other programs such as backup utilities or the make command-line interface programming utility. Typically these types of programs are only concerned with files which have been created or modified after the program was last run.
[root@server1 touch]# touch file
[root@server1 touch]# ls -l
total 0
-rw-r--r--. 1 root root 0 Mar 13 07:01 file

[root@server1 touch]# touch file1 file2 file3 file4
[root@server1 touch]# ls -l
total 0
-rw-r--r--. 1 root root 0 Mar 13 07:08 file1
-rw-r--r--. 1 root root 0 Mar 13 07:08 file2
-rw-r--r--. 1 root root 0 Mar 13 07:08 file3
-rw-r--r--. 1 root root 0 Mar 13 07:08 file4

[root@server1 touch]# touch ravi{1..10}.txt
[root@server1 touch]# ls -l
total 0
-rw-r--r--. 1 root root 0 Mar 13 07:09 ravi10.txt
-rw-r--r--. 1 root root 0 Mar 13 07:09 ravi1.txt
-rw-r--r--. 1 root root 0 Mar 13 07:09 ravi2.txt
-rw-r--r--. 1 root root 0 Mar 13 07:09 ravi3.txt
-rw-r--r--. 1 root root 0 Mar 13 07:09 ravi4.txt
-rw-r--r--. 1 root root 0 Mar 13 07:09 ravi5.txt
-rw-r--r--. 1 root root 0 Mar 13 07:09 ravi6.txt
-rw-r--r--. 1 root root 0 Mar 13 07:09 ravi7.txt
-rw-r--r--. 1 root root 0 Mar 13 07:09 ravi8.txt
-rw-r--r--. 1 root root 0 Mar 13 07:09 ravi9.txt

Create file using cat command

                          The program cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to catenate/concatenate and list files.

[root@server1 cat]# cat > filebycat
This file is created by cat command
[root@server1 cat]# cat filebycat
This file is created by cat command

Creating Directories

mkdir (make directory) command in the Unix, DOS and OS/2 and MS windows and other programs is used to make a new directory.

[root@server1 mkdir]# mkdir testdir
[root@server1 mkdir]# mkdir dirtest dir1
[root@server1 mkdir]# ls -l
total 0
drwxr-xr-x. 2 root root 6 Mar 13 07:16 dir1
drwxr-xr-x. 2 root root 6 Mar 13 07:16 dirtest
drwxr-xr-x. 2 root root 6 Mar 13 07:16 testdir


Listing files and directories

                        To list the files and directories we have to use 'ls' command. 'ls' command will list files and directories current directory, by providing the remote directory path will list all files and directories on that remote directory.

ls command with 25 practical examples

ls <Path>

[root@server1 mkdir]# ls
dir1  dirtest  testdir
[root@server1 mkdir]# ls -ltr
total 0
drwxr-xr-x. 2 root root 6 Mar 13 07:16 testdir
drwxr-xr-x. 2 root root 6 Mar 13 07:16 dirtest
drwxr-xr-x. 2 root root 6 Mar 13 07:16 dir1
[root@server1 mkdir]# ls -k
dir1  dirtest  testdir


Copy files and directories

                         cp is a UNIX command for copying files and directories. The command has three principal modes of operation, expressed by the types of arguments presented to the program for copying a file to another file, one or more files to a directory, or for copying entire directories to another directory.

we have to use -r option along with cp command to copy the directories.

cp <SourcePath> <DestinationPath>

[root@server1 ~]# cp file1 dir1/
[root@server1 ~]# cp testdir dir1/
cp: omitting directory ‘testdir’
[root@server1 ~]# cp -r testdir dir1/

Moving files and directories

                   mv (short for move) is a Unix command that moves one or more files or directories from one place to another. If both filenames are on the same filesystem, this results in a simple file rename; otherwise the file content is copied to the new location and the old file is removed. Using mv requires the user to have write permission for the directories the file will move between. This is because mv changes the content of both directories (i.e., the source and the target) involved in the move. When using the mv command on files located on the same filesystem, the file's timestamp is not updated.

mv <SourcePath> <DestinatioPath>

[root@server1 ~]# mv file3.txt /tmp/
mv: overwrite ‘/tmp/file3.txt’? y

[root@server1 ~]# mv dir1/ /tmp/
mv: overwrite ‘/tmp/dir1’? y
mv: cannot move ‘dir1/’ to ‘/tmp/dir1’: File exists


Deleting Files and Directories

               rm (short for remove) is a basic UNIX command used to remove objects such as files, directories, device nodes, symbolic links, and so on from the filesystem. To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file with two different names), and the objects themselves are discarded only when all references have been removed and no programs still have open handles to the objects.

[root@server1 ~]# ls
anaconda-ks.cfg  file1       file5.txt  file8.txt             mkdir  tech1  testdir   touch
cat              file10.txt  file6.txt  file9.txt             pipe   tech2  testdir1
dir1             file4.txt   file7.txt  initial-setup-ks.cfg  Pipe   tech3  testdir2

[root@server1 ~]# rm -rf file*

[root@server1 ~]# ls
anaconda-ks.cfg  dir1                  mkdir  Pipe   tech2  testdir   testdir2
cat              initial-setup-ks.cfg  pipe   tech1  tech3  testdir1  touch


We have a different file types in Linux we will discuss about few file types:


1. Regular Files (-)

we can create files using #touch command

[root@server1 ~]# ls -l |grep ^-
-rw-------. 1 root root 1292 Jan 19 23:14 anaconda-ks.cfg
-rw-r--r--. 1 root root 1343 Jan 19 23:15 initial-setup-ks.cfg
-rw-r--r--. 1 root root   31 Mar 13 04:54 Pipe


2. Directory Files (d)

Directories will start with d character we can search the directories using below command

[root@server1 ~]# ls -l /* |grep ^d
drwxr-xr-x. 2 root root       26 Jan 19 23:08 grub
drwxr-xr-x. 6 root root      104 Jan 19 23:14 grub2
drwxr-xr-x. 2 root root         200 Mar 13 01:16 block


3. Block File (b)

These files are hardware files most of them are present in /dev
find block file in Linux

ls -l /dev/* |grep ^b

[root@server1 ~]# ls -l /dev/* |grep ^b
brw-rw----. 1 root disk      8,   0 Mar 13 01:16 /dev/sda
brw-rw----. 1 root disk      8,   1 Mar 13 01:16 /dev/sda1
brw-rw----. 1 root disk      8,   2 Mar 13 01:16 /dev/sda2
brw-rw----. 1 root disk      8,  16 Mar 13 01:16 /dev/sdb
brw-rw----. 1 root cdrom    11,   0 Mar 13 01:16 /dev/sr0


4. Character device file (c)

Provides a serial stream of input or output.Your terminals are classic example for this type of files.

[root@server1 ~]# ls -l /dev/* |grep ^c
crw-rw-rw-. 1 root tty       5,   0 Mar 13 01:16 /dev/tty
crw--w----. 1 root tty       4,   0 Mar 13 01:16 /dev/tty0
crw--w----. 1 root tty       4,   1 Mar 13 01:18 /dev/tty1
crw--w----. 1 root tty       4,  10 Mar 13 01:16 /dev/tty10


5. Named Pipe file Or Just a pipe file (p)

The other name of pipe is a “named” pipe, which is sometimes called a FIFO. FIFO stands for “First In, First Out” and refers to the property that the order of bytes going in is the same coming out. The “name” of a named pipe is actually a file name within the file system.

Create pipe file using below command
# mkfifo pipe

[root@server1 ~]# ls -l |grep ^p
prw-r--r--. 1 root root    0 Mar 13 04:52 pipe


6. symbolic link file (l)

These are linked files to other files. They are either Directory/Regular File. The inode number for this file and its parent files are same. There are two types of link files available in Linux/Unix ie soft and hard link. 

#ls -l |grep ^l

7. Socket file (s)

A socket file is used to pass information between applications for communication purpose 

#ls -l |grep ^s


How did you feel about this article....?



No comments:

Post a Comment