ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to build a script that will filter a list of files by name and then do something to them if they begin with the certain letters.
For example, I have five files in /tmp: A1.txt, A2.txt, A3.txt, B1.txt, and B2.txt. I want to run one command on any file that startes with "A" and ends with "txt" and run another command on any other file that begins with "B" and ends with "txt."
I'm just not sure which one I should use. Thanks for the help!
If the output of the ls or find command is used as input, it can provide you with only the filenames (if any exist) that match your criteria, and present them to you in ascending alphabetic order. Your task is then reduced to describing what you need to do to just one file.
Look at some of the examples in "Common Tasks" in info find...
I'm having some problems doing this. I have another question... If I use this:
###############################
if file da*.dat; then
source /home/mijohnst/compiled_matlab_setup && /home/mijohnst/batchdownsamp "da*.dat"
mv -f *des.dat *.dat
tar -czvf $name.tgz *a*.dat --remove-files
done
################################
Will it run my source /home/mijohnst/compiled_matlab_setup && /home/mijohnst/batchdownsamp "da*.dat" command will it open and close it over and over through each file that starts with da*.dat or will it do them all while it's open?
If I am understanding your first question. you need to use a "for loop" in conjuntion with an "if then" statment. If I was you I would read up on those two constructs.
the for loop will go thru the whole directory, then the if then statment will decide what command to run on the file..
Sheesh... I'm stumped here! I've be looking at this all day and my mind hurts. Any someone help me out? This is what I've got... The red indicates where I'm lost.
#######################################
#
#/tmp/A-Files.tgz = A1.txt A2.txt and A3.txt
#/tmp/B-Files.tgz = B1.txt B2.txt and B3.txt
#
#######################################
# Global Verables
LIST=/tmp/tarlist.txt
# Start change
clear
echo "Creating List of Files"
ls /tmp/*.tgz >/tmp/tarlist.txt
echo ""
echo "Reading from $LIST for jobs to be desimated."
sleep 3
echo ""
for name in `cat ${LIST}` ; do
echo "Renaming $name..."
tar -xzvf $name
rm -f $name
if (files that start with A); then run this command
fi
if (files that start with B) then run this command
fi
done
echo "Repacking A Files..."
tar -czvf /tmp/A-Files-new.tgz ./A*.txt --remove-files
echo "Repacking B Files..."
tar -czvf /tmp/B-Files-new.tgz ./B*.txt --remove-files
echo ""
echo "Done!"
echo ""
exit 0
###################################
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.