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
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/
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?
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
ACCEPTED]
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.
cp, rm, mv - your current working directory will not be changed.Here [1] are some useful commands
[1] https://maker.pro/linux/tutorial/basic-linux-commands-for-beginnersAlso 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.
mvcommand changes the files location - how the actual filesystems implement that move will depend on the actual file systems, but on unix like systems themvcommand deals with the actual underlying processes, and the file disappears from the original location and appears in the new location.cpactually copies the file so you end up with two actual copies of the file on the filesystem. hope that helps - user86219cp --helpas that doesn't work on all *nix - just typingcpwithout 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