Linux Commands Continued...

In this section you will learn the Linux file, copy, removing commands.

Copying files with cp

[root@localhost ~] #cp original_file new_file
Linux copy command
Linux cp command

After pressing enter this command makes a new file, named new_file, with the same content as the original file.
In order to copy a file to a different directory, specify a path as the third word on the command line
# cp original_file /dir1/dir2/
Linux copy command
Linux cp command

if you want to copy with new name then specify the new name of file after the directory name.
# cp original_file /dir1/dir2/new_file
Linux copy command
Linux cp command

if you know where the file is and would like to place a copy of it in your current directory, enter the path as word two and . as the third word.
# cp /dir1/dir2/filename .
Linux copy command
Linux cp command

verbose mode.
Verbose mode is represented by -v . By using the cp command with verbose mode the running cp command can be displayed.
# cp -v /mohit/index.php /Linux.php
Linux copy command
Linux cp command

Some important options of cp command.
  • -p       Preserve file attributes if possible.
  • -r       Copy files recursively.
  • -R       Copy directories recursively.
  • -f       Force the overwriting of existing destination files without prompting.

Moving and Renaming Files and Directories

In Linux mv command is available for renaming the file as well as to move both files and directories to another location:
[root@localhost ~] # mv <original_file> <newname>
Linux mv file renaming
Linux mv file renaming
Notice that moving the file changed the filename but kept the same inode number and the timestamp value

You can also use the mv command to move directories:
mv [options] file destination

file will be moved at a time if the destination is a directory.
mv [options] file1 file2 destination
More than one file can be moved by specify file1, file1.. so on.
Linux mv file moving
Linux mv file moving command
The mv command with soft link and hard link problems, you'll learn in linking files section.

Creating and Removing Files

In order to create empty file, you can use touch command.
In order to remove file use rm command.
rm [options] <file>
# touch <file_name>

In order to remove file use rm command.
rm [options] <file>
  • rm -i file (interactive)
  • rm -r directory (recursive)
  • rm -f file (force)
Linux removing rm command
Linux rm command

Viewing file statistics

The stat command shows a complete rundown of the status of a file on the file system.
Viewing file statistics
Viewing file statistics

Viewing the file type

In order to determine kind of file it is, used command file.
Viewing the file type
Viewing the file type
The file command classifies files into three categories:
  • Text files: Files that contain printable characters.
  • Executable files: Files that you can run on the system.
  • Data files: Files that contain nonprintable binary characters, but that you can’t run on the system

Viewing the whole file

Consider you have a file with large content and you want to view the contents. There are three different commands in Linux that can help you out here.

The cat command

The cat command prints whole file.
Viewing the file using cat
Viewing the file using cat
There are a few parameters you can use with the cat command.
The -n parameter numbers all of the lines for us:
Viewing the row numbering using cat
Viewing the row numbering using cat
The above feature is useful when it comes to examin the scripts, if you want to know number of lines in the file then -b options is for you.
Viewing the number of lines cat
Viewing the number of line using cat
When you use cat command, once you start it, you can't control it. You will see its drawback on large file. In order to solve that problem there is new command more. The more command displays a text file, but stops after it displays each page of data.
Viewing file using more
Viewing file using more
The above figure shows that 71% of total file has been displayed. Next lines will be displayed after press the enter key.
One drawback of more command is, you can not move backward through a file, this problem is solved by less command. The lesscommand will allow you to move backward through a file as well as forward.
# less <file_name>
After pressing q you can get back to shell prompt.

Linking files

Linux file system allows you to make virtual copies of one file, that virtual copies are called linking of file. There are two different types of file links in Linux:
  • A hard link
  • A symbolic, or soft link

Hard links

The hard link creates a separate file that contains information about the original file and where to locate it. When you reference the hard link file, it's just as if you're referencing the original file
Linux hard link by ln
Linux hard link
The ln command is used for hard links in Redhat and CentOS cp -l does same purpose. In above figure home1.php is hardlink of home.php file. Inode of both file are same.
Note: You can only create a hard link between files on the same physical medium. You can't create a hard link between files under separate mount points. In that case, you'll have to use a soft link.

Soft link

The ln -s is used to create soft link. In Redhat and CentOS cp -s does same purpose.
Linux soft link by ln
Linux soft link
In above figure it is easy to detect the soft links.