How to extract, rename and view some log files from user inputed tar filename? - linux

The problem is like this :
I need to extract the logs from a tar archive using user input/argument for the file name (cubelog_457890.tar)
In the archive there is just one folder named tftpboot that I need to rename to the original user input.
After that I need to open and view the log files.
#!/bin/bash
fname=$1
if [ -f $fname ]; then
tar -xvzf $fname
fi
mv tftpboot $fname
If I try to use the script with the argument cubelog_457890.tar I have the problem that the MV line will not work.
Starting the script again and using cubelog_457890 will do the job.
How can I make the MV command take cubelog_457890 from user input without the tar extension?
./extract.sh cubelog_457890.tar - will extract but not rename
./extract.sh cubelog_457890 - will rename the folder

try this:
#!/bin/bash
fname=$1
if [[ -f "${fname}.tar" ]]; then
tar -xvf "${fname}.tar"
fi
mv tftpboot $fname
then ./extract.sh cubelog_457890

Related

How to get stdout of tar command

I am trying to tar a file and get it's output store in a variable.
I tried this but it is not working:
resulting_tar=$(tar -zcf "$(date '+%Y-%m-%d').tar.gz" folder)
Any idea how do I go about it?
By default, tar does not report the name of the file created. In fact, it doesn't say anything unless you tell it to, and the options given don't tell it to say anything.
Note that tar doesn't tell you what file it created. You tell tar what file to create.
You'll need to capture the name of the file in a variable and report it yourself:
file="$(date '+%Y-%m-%d').tar.gz"
tar -czf "$file" folder
echo "$file"
Try running tar -czf /dev/null folder; you won't see anything from (most implementations of) tar — and that's not because I specified /dev/null. Specify a name if you prefer: tar -czf junk.tar.gz folder and watch the (lack of) output — and remember to remove junk.tar.gz.
You might want to think about including the folder name in the tar file name, too.
folder="…whatever…"
file="$folder-$(date +'%Y-%m-%d').tar.gz"
tar -czf "$file" "$folder"
echo "$file"
EDIT: No longer applicable after further clarification. Leaving for posterity.
You're likely looking for both stdout and stderr. You can combine the two output streams by appending 2>&1 to your command:
resulting_tar=$(tar -zcf "$(date '+%Y-%m-%d').tar.gz" folder 2>&1)

Renaming files in subdirectories deletes the files

I am writing a script that renames *.MP4 files on an inserted SD card and then rsyncs them.
The directory with the *.MP4 files does not always have the same name:
eg: it could be /DCIM/123_PANA/ or /DCIM/141_PANA/ etc
So I'm trying to write a script that will see what folders are in the /DCIM path, and rename all the *.MP4 files, (there is also a MISC folder in this path which I suspect is causing the issue)
I am using a couple of variables to rename the files also
What I have is:
for f in /media/pi/LUMIX/DCIM/*; do
if [ -d "$f" ]; then
echo $f
for file in $(find $f -name 'P*.MP4')
do
echo $file ">" $(dirname "${file}")/$(date +"%d")$cardname$(basename $file)
mv $file $(dirname "${file}")/$(date +"%d")$cardname$(basename $file)
done
fi
done
But what seems to happen is I end up with a single file with the prefix only (say 08_nb1_) in the _PANA folder, all the others have been deleted. Obviously this is not my desired result!
UPDATE:
$cardname is of the format _nb2_
When I do as asked replace mv with echo here is the output:
/media/pi/LUMIX/DCIM/141_PANA
mv /media/pi/LUMIX/DCIM/141_PANA/P1410192.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410192.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410193.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410193.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410194.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410194.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410195.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410195.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410196.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410196.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410197.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410197.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410198.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410198.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410199.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410199.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410200.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410200.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410201.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410201.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410202.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410202.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410203.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410203.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410204.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410204.MP4
/media/pi/LUMIX/DCIM/MISC
OK I fixed it by filtering the additional directory names and limiting to only the ones with *_PANA - which solved the issue. I also added a the rsync part and demounted the SD card (if required using Zenity)
A text file placed on the SD card identifies it as a unique card giving each file a unique name when rsyncing it to the backup folder. Renaming on the SD card means
that it can still be used an written to if not full, but we then know which files have been backed up.
Very useful in the field when filming with multiple cards, crews. All running on a Rpi4
for f in /media/pi/LUMIX/DCIM/*_PANA/; do
if [ -d "$f" ]; then
echo "$f"
for file in $(find $f -wholename '*_PANA/P*.MP4')
do
mv "$file" $(dirname "${file}")/$(date +"%d")"$cardname"$(basename "${file}")
done
rsync --stats -u --progress "$f"/*.MP4 /media/pi/VDRIVE/ | tee /home/pi/Documents/ytu/rsync.txt | zenity --icon-name="dialog-warning" \
--width=300 --progress --pulsate --auto-close --auto-kill \
--title="Copying $sdn"
zenity --question --text="Unmount Card?"
if [ $? = 0 ]; then
umount /media/pi/LUMIX
else
exit
fi
fi
done

Extracting specific file types from all tar files from specific folder

I need shell script which accept two arguments.
First one is path to the specific folder and second one is int value (1 or 2).
If second argument is 1 then I have to go through all tar files in mentioned folder and extract just executable files into specific folder inside path from first argument. in this case name of that folder is "unpacked".
If second argument is 2 then I have to extract all *.txt files from all tar files from folder given by first argument.
I am trying something like this but don't know how to catch every tar file and extract one of these two file types.
#!/bin/bash
cd $1
if [$2 –eq 1 ]
then
for f in *.tar; do
tar –xv –f "$f" –-wildcards EXECUTABLE FILES -C ./unpacked
done
fi
if [$2 –eq 2 ]
then
for f in *.tar; do
tar –xv –f "$f" –-wildcards "*.txt" -C ./unpacked
done
fi
The [MEMBER...] argument must come last.
#!/bin/bash
cd $1
if [$2 –eq 1 ]
then
for f in *.tar; do
tar –xv –f "$f" –-wildcards -C ./unpacked EXECUTABLE FILES
done
fi
if [$2 –eq 2 ]
then
for f in *.tar; do
tar –xv –f "$f" –-wildcards -C ./unpacked "*.txt"
done
fi
To Extract specific files form a tar file execute in yor terminal:
$ tar -zxvf TARNAME.tar.gz PATH/FILNAME

Script create tar of a Parent directory which will have individual tar's of its sub directories

I want to write a script which will take two parameters
1. Name of Parent directory (ex. mainFolder)
2. The name of the tar to be created
The parent directory will have subfolers (folder1,folder2,folder3 etc).
I need a script which will create "mainFolder.tar" which will consists of "folder1.tar","folder2.tar","folder3.tar" ...etc...
tar cvf mainFolder.jar mainFolder
this will create tar of parent directly "mainFolder" only...Can anybody tell me how can I make mainFolder.tar to create & contain tar's of its sub directories...Need a Shell Script (ex. createTar.sh)which will do it..
while creating tar, only "directories" inside the parent folder "mainFolder" should be considered for tar..and not ".txt " files..
Here is a POSIX compliant solution for your problem. There may be a simpler way (not using any temporary files) but it would involve special and likely non-portable arguments to tar.
#!/bin/sh
startdir="$(pwd)"
mkdir -p parent/horse/frog parent/dog/frog parent/fish/frog
mkdir newparent
(
cd parent
for i in *
do if [ -d "$i" ]
then tar -cf "$startdir/newparent/${i}.tar" ${i}
fi
if [ -f "$i" ]
then cp "$i" "$startdir/newparent/"
fi
done
)
tar -cf newparent.tar newparent

Move files into their own directories

I have several hundred rar files. I would like to create a directory for each rar file then move the file into the newly created directory.
This is the code I am using to create the rar's
#!bin/bash
for f in *; do
rar a -s -m5 "${f%.*}.rar" "$f";
done
This is the code I am using to move the files.
#!/bin/bash
for i in *.rar; do
dir=$(echo "$i" | \
sed 's/\(.\)\([^ ]\+\) \([^ ]\+\) - \(.*\)\.pdf/\1\/\1\2 \3/')
dir="DestinationDirectory/$dir"
mkdir -p -- "$dir" && mv -uv "$i" "$dir/$i"
done
The problem is that it creates the directory with the extension name.
ie: file irclog3_26_198.rar is moved into folder /DestinationDirectory/irclog3_26_1988.rar/irclog3_26_1988.rar
I would like the folder to be created ignoring the .rar and just use the name of the file.
How about:
dir="${dir%.rar}"
mkdir -p -- "$dir" ...
Read more about it at the abs.
dir=$(echo ${i[#]::-4})
${name[#]:pos:len}) gets the substring/subarray of the string/array, for string, [#] can be avoid.
dir=$(echo ${i::-4})
You can use the normal bash shell parameter expension https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
FILE="TEST.rar"
echo "${FILE%%.*}"
--> TEST

Resources