Setting up Files And Directories

Linux includes specific commands for creating and deleting directories, file management and so on:-

1. touch:-
touch command is simply used for creating a new file in Linux. It creates an empty file under the current directory. For eg:-

# touch filename 


2. cp
the cp command is used to copy the contents from source to destination. For eg:-

# cp file1 file2
It simply copies the content of file1 to file2. cp command can be used as shown in table


                  Command                      Result
              cp file1 file2copies the content of file1 to file2. The destination file has new creation date and inode number.
              cp file* Dir1copies multiple files to directory
              cp -f file1 file2if file named with file2 already exists, than it overwrites its content without prompting.
              cp -i file1 file2if file named with file2 already exists, this command prompts confirmation before it overwrites its contents.
              cp -p file1 file2copies content of file1 to file2. The destination file has the same creation date and inode number as the source file
              cp -r Dir1 Dir2copies contents of directory1 to directory2. The effect is recursive; i.e if there are subdirectories under the Dir1, there files and directories are also copied.
              cp -u file1 file2If you already have file named file2 and file1 is newer, this command overwrites its contents without prompting.

Note:- An inode is the identifier used on each Linux partition for a file. Every file gets its own inode. The inode includes metadata about the file, which includes the permissions, size, last access time, and its disk block were it is located. If inode is misaligned or corrupted it won"t be able to find the associated file. In addition, identical files have the same inode number.

3. mv
mv stands for move and if you want to rename a file in the linux you move it. The mv command changes the name of the file.


            Command               Result
          mv file1 file2changes the name of a file from file1 to file2.
          mv file* Dir1Moves multiple files to the directory.
          mv -i file1 file2if file named file2 already exists, this command prompts you for confirmation before overwriting this file.
         mv -f file1 file2if a file named file2 already exists, this command overwrites its contents without prompting.


4. rm
rm is used to remove files and directories. This is one reason that Linux Administrators are not advised to run Linux in root or superuser mode, because small mistakes in this command can easily remove all your Linux files.lets take an example

suppose you want to remove a group of temporary directories in your root(/) directory: a.tmp, b.tmp, c.tmp. You want to use rm -r *.tmp command, but instead you type the following:

#rm -r * .tmp

Because there is space between the * and .tmp, the shell assumes that you want to recursively delete all directories and then delete the file name .tmp. And surely you don"t want to delete  all your directories and corresponding files and data.

For this reason, Red Hat configures the following as an allias for the root user:

alias rm=" rm -i "

the alias ensures that whenever you want to use the rm command (even rm -r), the shell prompts you for confirmation before you delete any other file.

TIP:- you can find default aliases with the alias command
             rm file 1Deletes file1 without prompting for confirmation.
            rm -d Dir1Deletes Dir1 without prompting for confirmation.
            rm -i file1Deletes file1 after prompting for confirmation from the user
            rm -f file2If a file name file2 already exists, this command overwrites its contents without prompting.
            rm -r *Removes file recursively, if there are any subdirectories in the current directory, this commands deletes them(and all their files) as well

5. ln
In Linux instead of just copying or moving a file, you can link it. Links are common, especially for those programs which run at different runlevels. While you link a file, you are actually creating another path to a currently existing file. Lets take an example

Suppose you and your friend or colleague are working on a file named project, you can create a linked file in your home directory. assume project file is in /home/jm directory. To create link use following command:

#ln  /home/jm/project /home/mj/project


When you work on either file, the change and results are visible and accessible to those who access both the directories. This sometimes known as hard link.

Note:- The ln file1 file2 commands produces the same result as cp -p file1 file2

Another useful option for links is symbolic mode which allows you to see the linked file.

# ln -s  /home/jm/project  /home/mj/projectyou will see the linked file when you run a long listing (ls -l) of that file. This is known as a soft link.

6.mkdir
mkdir command is used to create directories. The directory that you create does not have to be based in your current directory for this you have to specify the path. You can also assign permission to the directory.

# mkdir -p Dir1/Dir2
Creates a directory named Dir2. Id Dir1 does not exist, the -p switch tells Linux to create that directory as well. Since path is not specified so both are created as subdirectories of the current directory.

# mkdir -m 755 /usr/Dir3
Creates a directory named Dir3 as a subdirectory in the /usr directory(here the path is specified). The permissions(755) are rwx for the owner and r-x for other members of the group of group and everyone else.

7. rmdir
This commands allows you to delete empty directories.

# rmdir -p Dir1/Dir3
This command deletes Dir3 if it is empty. If the only "file" in directory Dir1 is Dir3, this command also deletes directory Dir1.

Comments

0 Responses to "Setting up Files And Directories"

Post a Comment

 

Managing Shell And its Secrets

Subscribe To Get Update On Mobile

Subscribe
Get updates of latest post on your mobile free