Deleting, Copying, Moving, Renaming , Transferring Files and Sorting.

Day 4

The last post was about creating users , creating files and directory.We have learn about how to create files and how to add users into groups , individuals. Now in this tutorial you will come know about File handling that copying,moving files by Linux commands. It is as simple as like Windows. So let\’s see….\nDay 4\n1. Copying Files :\nCopying Files in Linux is very simple as we copy files in Windows. We have Linux command cp which we can copy file from one location to other. The command is as shown below,\n# cp [source_file ] [destination_file]\nFor example, we want copy file located in / data/ program.c file to / myfile/form. The command is follow\n# cp /data/program.c /myfile/form/\nThe program.c file will copy into / myfile/form. If we copy file in same directory then we can give only the filename. Here we can use relative and absolute path. If we are in that directory where we want to copy file then we required a destination path.\nIf we are in different directory suppose on Desktop, then we have to provide both source path and destination path like we have used in above example.\n# cp myfile / data/ mydata/\nIn this example we are copying file from Desktop directory to mydata directory. Here we do not need to provide source path ,only the destination path is required.\n2. Deleting files \nHere we need command , when there unusable stuff in our system or externally we want to delete some files which are old. We have command for removing or deleting files is rm command.\n# rm [ filename_path ]\nHere we can used different options for removing files. The -r option is used remove the directory and its contents recursively. The -f option to remove files forcefully.\nBefore using this command we have to ensure that we are removing the files in which we want to delete. It\’s best practice to used ls command frequently to check list of files . So that we get an idea about removing files.\n3. Moving and renaming files\nIn Linux file system we have features of reming files ,moving files.We have mv command used to locate files from one location to another. It is also used for renaming also.\n# mv source_file destination_file\nFor example, we want to rename a filename locate in a directory / data/ myfile to demofile ,the command we use is as follows,\n# mv /data/myfile demofile\nHere, we have renamed filename from myfile to demofile. Similarly, we want to move a /mydirectory/demo file to /myfile/graph directory the command will be\n# mv /mydirectory/demo /myfile/graph\nWe also -i option interactive mode ,if the destination exists, mv will prompt you before it overwrites the file.\n4.Transferring files using scp\nWe have seen cp command to copy files on local machine. But what if we want to copy files from one machine to another? In Linux we have some protocols used for transferring files from local machine to remote host, or two remote hosts.\nWe used SCP (secure copy protocol) and SFTP(secure file transfer protocol) protocols for transferring files. The command is as below\n#scp source destination\n#sftp [username@]host\nThe first command copy file from source to destination. The second command connect a host as username to begin secure transfer.\nscp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2\nFor example, we want copy file from one user location like linuxuser1 and his directory /home/linuxuser1/usrdir.txt it to another linuxuser2 and his home directory /home/linuxuser1/.The command will be\n#scp linuxuser1:~/usrdir.txt linuxuser2: ~/\nIn this way we can transfer files between two hosts.\n5. Sorting Files\nSort command is used sort the files on the base of ASCII values. We can different option with the sort command, it can be used sort numerically. Sort command sort the contents numerically , command line utility for sorting lines of text files. It can also supports the sorting files alphabetically, in reverse, by number,etc. We will discuss some examples of sort command with option as shown below.\nSuppose we have file with data.txt ,we will apply sort command to get sorted list.\n# cat data.txt\nMango\nPineapple\nOrange\nStrawberry\nApple\nBanana\nWatermelon\nNow used sort command to get output\nCommand :\n# sort data.txt\nOutput :\nApple\nBanana\nMango\nOrange\nPineapple\nStrawberry\nWatermelon\nThe file is sorted. We can some options to get more specific data. The -o option will allows you to specify a output file. We can redirect a output into new file.\n#sort data.txt > newfile.txt\n#sort -o newfile.txt data.txt\n#cat new file.txt\nOutput :\nApple\nBanana\nMango\nOrange\nPineapple\nStrawberry\nWatermelon\nHere we have redirected output into newfile.txt. We have another option like -r ,we can sort file using reverse order. Also we have -n option for sort file numerically.\nCommand :\n# sort -r data.txt\noutput :\nWatermelon\nStrawberry\nPineapple\nOrange\nMango\nBanana\nApple\nWe sort numeric data by – n option we have file numbers.txt ,we can sort by using command as follow\nCommand :\n#cat > numbers.txt\nOutput :\n30\n40\n54\n12\n25\n70\nCommand :\n# sort -n numbers.txt\nOutput :\n12\n25\n30\n40\n54\n70\nWe can also used -nr option together for sort file numeric data in reverse order.\nCommand :\n# sort -nr numbers.txt\nOutput :\n70\n54\n40\n30\n25\n12\nIn this way ,we can sort the files,edit files by using Linux commands\n\” In next post we will see the file editing using vi editor,compressing files and job scheduling. \”The last post was about creating users , creating files and directory.We have learn about how to create files and how to add users into groups , individuals. Now in this tutorial you will come know about File handling that copying,moving files by Linux commands. It is as simple as like Windows. So let’s see….

Day 4

1. Copying Files :

Copying Files in Linux is very simple as we copy files in Windows. We have Linux command cp which we can copy file from one location to other. The command is as shown below,

# cp [source_file ] [destination_file]

For example, we want copy file located in / data/ program.c file to / myfile/form. The command is follow

# cp /data/program.c /myfile/form/

The program.c file will copy into / myfile/form. If we copy file in same directory then we can give only the filename. Here we can use relative and absolute path. If we are in that directory where we want to copy file then we required a destination path.

If we are in different directory suppose on Desktop, then we have to provide both source path and destination path like we have used in above example.

# cp myfile / data/ mydata/

In this example we are copying file from Desktop directory to mydata directory. Here we do not need to provide source path ,only the destination path is required.

2. Deleting files

Here we need command , when there unusable stuff in our system or externally we want to delete some files which are old. We have command for removing or deleting files is rm command.

# rm [ filename_path ]

Here we can used different options for removing files. The -r option is used remove the directory and its contents recursively. The -f option to remove files forcefully.

Before using this command we have to ensure that we are removing the files in which we want to delete. It’s best practice to used ls command frequently to check list of files . So that we get an idea about removing files.

3. Moving and renaming files

In Linux file system we have features of reming files ,moving files.We have mv command used to locate files from one location to another. It is also used for renaming also.

# mv source_file destination_file

For example, we want to rename a filename locate in a directory / data/ myfile to demofile ,the command we use is as follows,

# mv /data/myfile demofile

Here, we have renamed filename from myfile to demofile. Similarly, we want to move a /mydirectory/demo file to /myfile/graph directory the command will be

# mv /mydirectory/demo /myfile/graph

We also -i option interactive mode ,if the destination exists, mv will prompt you before it overwrites the file.

4.Transferring files using scp

We have seen cp command to copy files on local machine. But what if we want to copy files from one machine to another? In Linux we have some protocols used for transferring files from local machine to remote host, or two remote hosts.

We used SCP (secure copy protocol) and SFTP(secure file transfer protocol) protocols for transferring files. The command is as below

#scp source destination
#sftp [username@]host

The first command copy file from source to destination. The second command connect a host as username to begin secure transfer.

scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2

For example, we want copy file from one user location like linuxuser1 and his directory /home/linuxuser1/usrdir.txt it to another linuxuser2 and his home directory /home/linuxuser1/.The command will be

#scp linuxuser1:~/usrdir.txt linuxuser2: ~/

In this way we can transfer files between two hosts.

5. Sorting Files

Sort command is used sort the files on the base of ASCII values. We can different option with the sort command, it can be used sort numerically. Sort command sort the contents numerically , command line utility for sorting lines of text files. It can also supports the sorting files alphabetically, in reverse, by number,etc. We will discuss some examples of sort command with option as shown below.

Suppose we have file with data.txt ,we will apply sort command to get sorted list.

# cat data.txt
Mango
Pineapple
Orange
Strawberry
Apple
Banana
Watermelon

Now used sort command to get output

Command :
# sort data.txt
Output :
Apple
Banana
Mango
Orange
Pineapple
Strawberry
Watermelon

The file is sorted. We can some options to get more specific data. The -o option will allows you to specify a output file. We can redirect a output into new file.

#sort data.txt > newfile.txt

#sort -o newfile.txt data.txt

#cat new file.txt

Output :

Apple

Banana

Mango

Orange

Pineapple

Strawberry

Watermelon

Here we have redirected output into newfile.txt. We have another option like -r ,we can sort file using reverse order. Also we have -n option for sort file numerically.

Command :

# sort -r data.txt

output :

Watermelon

Strawberry

Pineapple

Orange

Mango

Banana

Apple

We sort numeric data by – n option we have file numbers.txt ,we can sort by using command as follow

Command :

#cat > numbers.txt

Output :

30

40

54

12

25

70

Command :

# sort -n numbers.txt

Output :

12

25

30

40

54

70

We can also used -nr option together for sort file numeric data in reverse order.

Command :

# sort -nr numbers.txt

Output :

70

54

40

30

25

12

In this way ,we can sort the files,edit files by using Linux commands

” In next post we will see the file archiving,compressing files and job scheduling. “

Leave a comment