[Webinar] Learn how to a build a cloud-first strategyRegister Now


SUSE 10.0 xargs and opening different file extensions

Posted on 2006-04-04
Medium Priority
Last Modified: 2012-05-05
I am using SUSE 10.0.  I have some files that appear either as .zip or .txt randomly.  Each file has a date / time stamp connected to them.  They are in the following format.


    The "BOBapp04-04-2006@19-00-" are standard.  I can add in a date function to that portion of the file.  The 05 is the second and that seems to be random within 20 seconds depending on when the script actually runs.  I would like to determine if it is a txt or zip, extract if it is a zip, then change the file name to something that is not random (but keep the date functionality).

Does anyone have any recommendations?  I'm giving alot of points for this as I want to test the script to get it working before giving out the points!

Question by:awakenings
  • 3
  • 2
LVL 15

Expert Comment

ID: 16377600
Can you elaborate on "something that is not random" ?

Everything you mentioned so far is a one-liner.

Author Comment

ID: 16377612
I would love the file name to be;

BOBapp04-04-2006@19-00.txt for example.
LVL 15

Assisted Solution

Tim_Utschig earned 2000 total points
ID: 16377655
for Z in *.zip; do unzip "$Z"; done
for T in *\@[0-9][0-9]-[0-9][0-9]-[0-9][0-9].txt; do \mv -i "$T" "${T%-??.txt}.txt"; done

Author Comment

ID: 16381177

      I am testing things here.  You will have to forgive the fact I am a little slow.  I am a windows guy attempting to learn Linux.  I did some testing with some of this.  I could type in unzip BOBapp04-04-2006* and it would unzip the file.  I could type in mv BOBapp04-04-2006@15-00-??.txt app04-04-06.txt and the file would rename.  What I do not get is how do I collect random files and if they are zip, unzip and rename themthem and if they are txt file just rename them.  I was told the xargs command could do that.  Do you have another suggestions?

     I must admit I did not fully understand the statements above as I am no programmer.  How do I take this stuff and make it into a cron job?  I have the date functionality down so that will be easy.  I know how to do a cron job, but I am fuzzy on taking the random endings (txt or zip) and turn them into just functional txt file.  I then intend to run another cron job to run the files through swatch.  I am trying to automate a process to make things easier for me.


LVL 15

Accepted Solution

Tim_Utschig earned 2000 total points
ID: 16397638
You would only need xargs if you are going to have more than 64 KB worth of filenames, at that point *.txt or *.zip will make the argument list too long.
If you run in to that problem, you could use these commands instead of my last example:

    find . -name \*.zip -print0 | xargs -0 -n1 unzip
    find . -name "*@[0-9][0-9]-[0-9][0-9]-[0-9][0-9].txt" -print0 | xargs -0 -n1 sh -c 'mv -i "$1" "${1%-??.txt}.txt"' none

> What I do not get is how do I collect random files and if they are zip

Shell wildcards let you pass filename(s) using wildcards.   Try this out:

    echo Hello World
    echo *.zip
    echo *.txt

Another method, usually used only when you're going to have massive amounts of files, is find piped to xargs.  E.g:

   find . -name "*.txt" | xargs echo
   find . -name "*.txt" | xargs -n1 echo

xargs takes the filenames piped to it and passes them to the command you specify, running the command multiple times if the argument list is too long.

To add the commands to a cron job, just put them in a shell script, chmod +x it, and specify the full path to it in your cron job.
To create a shell script, simply create a file, put the following line at the top:


and put your commands after it.   Since a cron job will start in your home directory, you'll probably need to have a cd command which changes to the directory where the files are located before executing your commands.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month20 days, 9 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question