Home > Back-end >  How to extract, rename and view some log files from user inputed tar filename?
How to extract, rename and view some log files from user inputed tar filename?

Time:08-19

The problem is like this :

  1. I need to extract the logs from a tar archive using user input/argument for the file name (cubelog_457890.tar)
  2. In the archive there is just one folder named tftpboot that I need to rename to the original user input.
  3. 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

CodePudding user response:

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