Tar files in one location - unix

At 10 A.M files are like below say in one of the location
A.log,B.log
Tar file should contain A.log and B.log. Tar file name should be like
archival_file__oror_datetimestamp.tar
At 11 A.M if files are like below
A.log,B.log,C.log
Now the tar file which is getting created now should not contain A.log and B.log it should contain C.log only

If I correctly guess the intention of your question you might want to do something like
# 10AM:
tar -cf /path/to/tarfile/archival_file__oror_10AM.tar /path/to/logfiles/*.log
# 11 AM
tar -N /path/to/tarfile/archival_file__oror_10AM.tar -cf /path/to/tarfile/archival_file__oror_11AM.tar /path/to/logfiles/*.log
The second command will only put files newer than archival_file__oror_10AM.tar into the new archive.

Related

Archiving multiple files in Unix using tar and gzip

I have the requirement to archive multiple files keeping the original files using tar and gzip. I cannot take risks with the files I have.
For example, the files to be archived are:
ls
1.doc
2.doc
3.doc
4.xls
5.xls
6.xls
The expected output:
ls
1.doc
2.doc
3.doc
4.xls
5.xls
6.xls
archive.tar.gz
Where archive.tar.gz file contains all the doc and xls files.
Did you try the command:
tar czf archive.tar.gz *.doc *.xls
Options here are:
c: Create
z: Gzip
f: Output file
To extract:
tar xzf archive.tar.gz
You can read the manual of the tar command for advanced options:
man tar

How to extract a .tar.gz file on UNIX

I have a file (reviews_dataset.tar.gz) that contains many files which contains data. I am required to extract the files in this archive and then perform some basic commands on the file. So far I have created a directory named (CW) and found a command tar zxvf fileNameHere.tgz but when I run this it of course cannot find my file as I have not "downloaded it" into my directory yet? How do I get this file into my directory so that I can then extract it? Sorry if this is poorly worded I am extremely new to this.
You must either run the command from the directory your file exists in, or provide a relative or absolute path to the file. Let's do the latter:
cd /home/jsmith
mkdir cw
cd cw
tar zxvf /home/jsmith/Downloads/fileNameHere.tgz
You should use the command with the options preceded by dash like this:
tar -zxvf filename.tar.gz
If you want to specify the directory to save all the files use -C:
tar -zxf filename.tar.gz -C /root/Desktop/folder

Extract tar file without creating folder

I want to extract tar file in Unix tar xvf /home/test/group.tar and once extracted got a folder group which has list of xls,pdf,txt files.
How can i extract contents of group.tar inside /home/test/list of xls, pdf files without creating group folder.
Any specific command available or have to follow with copy and move??
Thanks!
You may use the --strip-components 1 parameter.
tar xvf group.tar --strip-components 1

Unix- Copying the same file from multiple directories into a new directory while renaming the files

I have 36 subdirectories in the same directory named 10,11,12,....45 and a subdirectory logs
in each subdirectory (except for the directory logs) there is the same file called log.lammps
i was wondering if there was a way where i could copy each log.lammps file from each subdirectory 10-45 and put it in the sub directory logs while also adding the number of the directory that it originated from to the end of the filename
so i am looking for a code that copies the file log.lammps one by one from each subdirectory and every time the file gets copied into the directory logs, the filename gets changed from log.lammps to log.lammps10 if it came from the subdirectory 10 and when the file log.laamps from subdirectory 11 is copied into logs its name changes to log.lammps11 etc.
any help would be appreciated since right now i am only dealing with 30-40 files and in time i will be working with hundreds of files
Something along this line should work:
for f in [0-9][0-9]/log.lammps; do
d=$(dirname ${f})
b=$(basename ${f})
cp ${f} logs/${b}.${d}
done
That's easy-peasy with the magic of shell scripting. I'm assuming you have bash available. Create a new file in the directory that contains these subdirectories; name it something like copy_logs.sh. Copy-paste the following text into it:
#!/bin/bash
# copy_logs.sh
# Copies all files named log.lammps from all subdirectories of this
# directory, except logs/, into subdirectory logs/, while appending the name
# of the originating directory. For example, if this directory includes
# subdirectories 1/, 2/, foo/, and logs/, and each of those directories
# (except for logs/) contains a file named log.lammps, then after the
# execution of this script, the new file log.lammps.1, log.lammps.2, and
# log.lammps.foo will have been added to logs/. NOTE: any existing files
# with those names in will be overwritten.
DIRNAMES=$( find . -type d | grep -v logs | sed 's/\.//g' | sed 's/\///g' | sort )
for dirname in $( echo $DIRNAMES )
do
cp -f $dirname/foo.txt logs/foo$dirname
echo "Copied file $dirname/foo.txt to logs/foo.$dirname"
done
See the script's comments for what it does. After you've saved the file, you need to make it executable by commanding chmod a+x copy_logs.sh on the command line. After this, you can execute it by typing ./copy_logs.sh on the command line while your working directory is the directory that contains the script and the subdirectories. If you add that directory to your $PATH variable, you can command copy_logs.sh no matter what your working directory is.
(I tested the script with GNU bash v4.2.24, so it should work.)
For more on bash shell scripting, see any number of books or internet sites; you might start with the Advanced Bash-Scripting Guide.

Unix command to create a tar of a specified number of files

I am looking for a Unix command which will create a tar of 10 files from a directory.
tar cf path_of_tar.tar $(ls | head -10)
Add options to ls to select the 10 you want.
The command you're looking for is: tar
How it's usually used:
$ tar cf file.tar file1 file2...
Well, depending on your needs...
$ tar cf tenfiles.tar file1 file2 file3 ... file10
That'll do it. You can check out the tar manpage ($ man tar) for further details on other options you might need. (Your question was a bit vague, so I can't be that much more specific.)
I would suggest trying:
man tar
This will show all the options available and usage information. A typical usage for creating a tar of files in a directory would look like this:
tar -cvf myfiles.tar ./mydirectory
where myfiles.tar is the name of the tar file you want to create, and mydirectory is the directory the files reside in.
tar -cvf name.tar /path/to/file1 /path/to/file2 /path/to/file3 ...
Can you define what files are they? Are they of a specific filename pattern? My reasoning is asking that you specified 10 files.
In general:
tar cvf tar_with_10_files.tar somefile_with_wildcards_or_pattern_matching

Resources