3

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

3 Answers 3

6

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.

3
  • Thank you for your quick response. what is the difference between cp and mv? if I use mv command then will I need to use command to remove?
    – Bashabi
    Commented Apr 17, 2020 at 15:12
  • @Bashabi the mv command changes the files location - how the actual filesystems implement that move will depend on the actual file systems, but on unix like systems the mv command deals with the actual underlying processes, and the file disappears from the original location and appears in the new location. cp actually copies the file so you end up with two actual copies of the file on the filesystem. hope that helps
    – user86219
    Commented Apr 17, 2020 at 20:03
  • @santosh-garole I removed the bit about cp --help as that doesn't work on all *nix - just typing cp without arguments will cause the system to give some sort of usage, but I wasn't sure how to word that. Thanks for your edit.
    – user86219
    Commented Apr 17, 2020 at 20:18
0
  1. After performing commands like cp, rm, mv - your current working directory will not be changed.

Here are some useful commands

0

Also mind permissions (user, group, world) and ownership, that's why one answer uses sudo cp instead of only cp – files under /var/log are very likely only readable by root. In that case sudo cp also copies permissions and sets ownership to root user. Thus a file in /var/log owned by user apache, group apache with access rights 600 (only readable by owner, not by group and also not by rest of the world) ends up being owned by root and also only readable by root…in your user directory.

So maybe check ownership in target directory with ls -l and fix with sudo chownand/ or sudo chmod – I assume this introduces a security risk, exposing log files with different permissions in different owner's folder. Think about archiving and/ or deleting properly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .