Linux Commands – How to Copy, Read, and Remove Files

cpfileslinuxlogsrm

I want to copy some files from root directory (/var/log/) to home directory(home/test/copyfromlogs/) and after that i want to remove those files from root directory.

The files I want to copy are situated under /var/log/. The root directory is filling up so I want to remove the following files from there. .

btmp-20200401 >> 894M ; secure-20200322 >> 187M ; secure-20200329 >> 235M ; secure-20200405 >> 180M ; secure-20200412 >> 119M

I have created directory under home to have a backup of those file so that i need them just incase. The full path of the new directory is '/home/test/copyoflogfiles/'

I am new learner. I want to ask

  1. If the following command is correct if I want to copy btmp-20200401 from /var/log to /home/test/copyoflogfiles/. If not what will be the correct command

    cp /var/log/btmp-20200401 /home/test/copyoflogfiles/

  2. What will be my current directory when I will perform the copy command? Suppose I am inside /home/test/copyoflogfiles/ . In that case will the command be different?

  3. Can you please tell me what is the command for deleting single file from the directory . I want to remove the file btmp-20200401from /var/log/ after copying that file

Kind regards

Best Answer

Question 1: your command is correct:

cp /var/log/btmp-20200401 /home/test/copyoflogfiles/

If you do not have the file system privileges to copy the file, then the sudo command can be used to elevate your permissions, for example:

sudo cp /var/log/btmp-20200401 /home/test/copyoflogfiles/

Question 2:

You can use the cp command from any directory to any other directory if you are using full paths so you could run that command in any other directory.

Question 3:

rm /var/log/btmp-20200401

Would remove that file, to be sure you could use rm -i filename which will prompt you for the correct file.

However it might be better to use the mv command rather than cp followed by rm

So your command would change to:

mv /var/log/btmp-20200401 /home/test/copyoflogfiles/

Which would move the file rather than a copy and delete.

Related Question