LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-02-2010, 08:34 AM   #1
phual
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Rep: Reputation: 0
Bash partial directory content


I've got a fairly simple problem to be solved. I have a number of directories that contains files with different extensions. I want to run a script that gives different outputs for a given extension within a chosen directory.

E.g.

For directory 1, which contains:

about.xyz
file1.abc
file1.txt
file2.abc
file2.txt
file3.abc
file3.txt
support.xyz

output:

./rundifferentscript -files file1.abc file2.abc file3.abc -extra about.xyz support.xyz


Should be simple... can I figure it? Can I heck! Sorry for the dumb question - I just can't seem to wrap my head around bash.

Stuart
 
Old 04-02-2010, 08:44 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I am not clear on what you need to do. Are you trying to sort the names? Or maybe you need to have the script take an action based on the name?

What you show as "output" just looks like how the command might be entered.

Do you have some kind of BASH tutorial?---If not, go to http://tldp.org and get the Bash Guide for Beginners
 
Old 04-02-2010, 10:49 AM   #3
phual
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Original Poster
Rep: Reputation: 0
I have a program (which I didn't write) which runs with a number of different input files. There are around six different file types that can be used, each file type being used differently by the script, and the script can utilise any number of each file type (usually one, but sometimes more). Each collection of files that go together are stored in their own directory (including one or more files of different file type), and when I run my script I want it to work on all files in that directory.

At the moment, I have to call the script by manually typing:

Code:
./rundifferentscript -files directory1/file1.abc directory1/file2.abc directory1/file3.abc -extra directory1/about.xyz directory1/support.xyz
As you can imagine, it gets tedious to do it this way. What I want to do is run a script where I specify the directory and it calls my other script with the parameters determined automatically, so that I only need to type:

Code:
./newscript directory1
I've tried a couple of times to work my way into bash, but I don't think that I'm going to manage it without a significant amount of time learning bash properly and motivation in the form of a more sustained need for the knowledge (my needs are simple and sporadic). My attempts to find answers myself never seem to work. For example, I've found that echo *.abc almost does the job (returning "file1.abc file2.abc file3.abc"), but I'd like to be able to do more manipulation than *.abc allows (such as prefix each file with a path).

Thanks for the link through. I'll need to sit down and give it some attention.

Stuart
 
Old 04-02-2010, 10:49 AM   #4
IW2B
Member
 
Registered: Aug 2008
Location: Denmark
Distribution: Fedora, Ubuntu, Solaris
Posts: 35

Rep: Reputation: 19
Hi Stuart,

For your script/command "rundifferent" to know what is -files and which is -extra you must either hard code the value that you want or accept it as a command parameter.

Sample Code:

Code:
#!/bin/bash
if [ $# -lt 1 ]; then
	 	echo "Usage $0 <extension>"
else
	echo "You specified an extension off $1"
	echo
	echo "files : Files matching $1 extension:"
	ls  -1 *.$1
	echo
	echo "extra : Files not matching $1 extension"
	ls -1  | grep -v \.$1
fi


Sample output:

Code:
$ sh ./rundifferent.sh 
Usage ./filelist.sh <extension>

$ sh ./rundifferent.sh abc
You specified an extension off abc

files : Files matching abc extension:
file1.abc
file2.abc
file3.abc

extra : Files not matching abc extension
about.xyz
file1.txt
file2.txt
file3.txt
rundifferent.sh
support.xyz

$ sh ./rundifferent.sh txt
You specified an extension off txt

files : Files matching txt extension:
file1.txt
file2.txt
file3.txt

extra : Files not matching txt extension
about.xyz
file1.abc
file2.abc
file3.abc
rundifferent.sh
support.xyz

$ ls -1
about.xyz
file1.abc
file1.txt
file2.abc
file2.txt
file3.abc
file3.txt
rundifferent.sh
support.xyz
This is just a quick script to show how I understood your question, I have not fully tested it.

Hopefully it will give you an idea/help you.

Regards
Ian
 
Old 04-02-2010, 11:38 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
I've tried a couple of times to work my way into bash, but I don't think that I'm going to manage it without a significant amount of time learning bash properly and motivation in the form of a more sustained need for the knowledge (my needs are simple and sporadic).
I am going to divulge the secrets to learning BASH:
One command at a time
man -k <keyword>
man <command>
trial and error
 
Old 04-02-2010, 12:39 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by pixellany View Post
I am going to divulge the secrets to learning BASH:
One command at a time
man -k <keyword>
man <command>
trial and error
Very good advice; so much of using bash is automating execution of commands; to do that successfully you need to know a) what the commands are b) how to use them and c) what they output.

bash as a programming language is relatively small (but non-intuitive) so getting started with it is relatively easy (mastering its nuances may be more than many of us can do in a life time!).

If you have used C++ in a Windows environment, the situation is analogous; C++ itself is relatively simple while the Win32 interface libraries are immense and complex. Mastering the nuances of C++ may be easier than bash, though.
 
Old 04-02-2010, 12:55 PM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
And you might want to run pinfo find with special attention to the "Action" section. (Install the pinfo command if you haven't already done so. In my opinion it provides a much better interface to the info files.)

My point is that you may not need to use a bash script to accomplish your goal.
 
Old 04-03-2010, 08:36 AM   #8
phual
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Ian, interesting as it is the code you produced doesn't quite capture the problem fully due to the way in which it outputs the information (a new line per result).

From the general responses it does look as though I'm going to have to do it the hard way and do lots of reading.

Thanks anyway.

Stuart
 
Old 04-04-2010, 02:31 PM   #9
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Using Ian's code, here's one way to do it:
Code:
#!/bin/bash
if [ $# -lt 1 ]; then
  echo "Usage $0 <extension>"
  exit 0
fi
# For testing, just copy the temp file to stdout.
# For "real," use something like tmp=/tmp/sdf.sh
tmp="/dev/stdout"
echo '#!/bin/bash' >$tmp
echo -n ". $PWD/rundifferentsript -files " >$tmp
for f in $(ls  -Q *.$1)
do
  echo -n "$f"' ' >$tmp
done
echo -n "-extra " >$tmp
for f in $(ls -Q --ignore='*.'"$1")
do
  echo -n "$f"' ' >$tmp
done
echo >$tmp
# To run the temp file, uncomment the following (the "rm" is for housekeeping).
#chmod u+x $tmp
#. $tmp
#rm -f $tmp
And here's a sample output from my bash scripts directory:
Code:
$ ./Ian.sh sh
#!/bin/bash
. /home/Peter/Scripts/bash/rundifferentsript -files "Convert to ext4.sh" "Ian.sh" "run-mozilla.sh" -extra "child" "Emerald_replace" "main" "mount_all_images_in" "new_script" "RepairDamagedCD" "Uncompress_Books
Note that, instead of the echo -n command used to output the text without a new-line at the end, you could, instead, use a "\" as the last character in file line and get something like this:
Code:
#!/bin/bash
. /home/Peter/Scripts/bash/rundifferentsript -files "Convert to ext4.sh" \
"Ian.sh" \
"run-mozilla.sh" \
-extra "child" \
"Emerald_replace" \
"main" \
"mount_all_images_in" \
"new_script" \
"RepairDamagedCD" \
"Uncompress_Books \
(You'd also need an "extra" echo after you echoed the last "extra" file name to terminate the command input.)

Last edited by PTrenholme; 04-04-2010 at 02:33 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
configuring apache for refusing partial content transfer linuxsavar Linux - Server 0 02-19-2009 03:40 AM
How to hide content of /home directory? krivenok Linux - Security 5 09-14-2006 09:57 PM
The postgresql data directory content in the pgsql directory is lost (empty data dir) kisembo Linux - Software 1 02-13-2006 01:11 PM
directory content on apache spank Linux - Newbie 1 09-23-2005 02:40 PM
How to chow a directory and all of it's content? ReSync Linux - General 4 06-30-2002 04:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration