Linux - how to copy, remove, archive files

Post date: Mar 31, 2013 7:54:57 PM

Extracting a TAR

Extract linux tar gz (Gzip) archive

tar -xzvf mystuff.tgz

Extract linux simple tar archive

tar -xvf mystuff.tar

Extract linux tar archive to speciefied directory

tar -xvzf filename.tar.gz -C /desired/path

Remove files and directories

If directory is not empty

If you want to remove a directory with all its contents, you can use rm with the -r option. The -r option tells rm to remove a directory recursively:

$ rm -r dir1

Above command won't prompt you before removing. If you want a warning use following::

$ rm -ir dir1

If directory is empty

$ rmdir dir1

Copy or move

Standard format

$ cp -r dir1 dir2

To copy file1.txt in the current directory to the newdir directory.

cp file1.txt newdir

To copy a file with different name

cp /home/public_html/mylog.txt /home/public_html/backup/mylog.bak

[Copies the mylog.txt file in the public_html directory into the public_html/backup directory as mylog.bak. The files are identical however have different names.]

Copy all fines with an extension

cp *.txt newdir

[Copy all files ending in .txt into the newdir directory.]

Copy all files and directories

cp -r /home/hope/files/* /home/hope/backup

[Copies all the files, directories, and subdirectories in the files directory into the backup directory.]

Copies with overwrite

yes | cp /home/hope/files/* /home/hope/files2

[Copies all the files and subdirectories in files into the files2 directory. If files with the same name exist or it's prompted to overwrite the file it answers yes.]