PDA

View Full Version : Bash Terminal Command Question



MechanicalMan64
09-25-2008, 06:41 PM
Hey All, just wondering if I want to write a regular expression that will exclusively match files that start with DSCN followed by any number between 812 and 963 followed by .jpg, how should that command look like?

the "ls *<file name>" ones....

thanks

MechanicalMan64
09-26-2008, 08:07 AM
bump~

cdnLilWolf
09-26-2008, 12:56 PM
Here is a hint.

ls -l | grep "DSCN"

http://www.ss64.com/bash/ls.html

The above is a decent reference guide to BASH commands.

MechanicalMan64
09-26-2008, 02:42 PM
Here is a hint.

ls -l | grep "DSCN"

http://www.ss64.com/bash/ls.html

The above is a decent reference guide to BASH commands.

but that doesn't define the specific range?

Angre Nichols
09-26-2008, 03:52 PM
Guess that's why it was only a hint... unless he was trying to be smart about it...

Angre Nichols
09-26-2008, 04:03 PM
Would this work?

To look for the pattern "hi" in the files, myfile1, myfile2, myfile3, and myfile4, you can use:
grep myfile[1234]
or
grep myfile[1-4]
so

grep filename[812-963]

cdnLilWolf
09-26-2008, 04:34 PM
I was not trying to be smart, it's just that I don't do other peoples homework. If they post what they tried (unsuccessfully), I would be happy to point them in the right direction. That's how one learns.

MechanicalMan64 did not demonstrate any attempt at trying. I was merely encouraging him to do so.

MechanicalMan64
09-26-2008, 06:51 PM
Would this work?

To look for the pattern "hi" in the files, myfile1, myfile2, myfile3, and myfile4, you can use:
grep myfile[1234]
or
grep myfile[1-4]
so

grep filename[812-963]

i tried this, it gives me a command error?

cdnLilWolf
09-26-2008, 06:52 PM
By the way MechanicalMan64, have you tried....?

ls DSCN[812-963].jpg

PS. Make sure your syntax includes your path if you are not local to the directory.

MechanicalMan64
09-26-2008, 07:39 PM
By the way MechanicalMan64, have you tried....?

ls DSCN[812-963].jpg

PS. Make sure your syntax includes your path if you are not local to the directory.

i am accessing it locally, and it gives me the following message:

"ls: cannot access DSCN[812-963].jpg: No such file or directory"

i'm pretty sure i'm in the right directory.
what's going on?>

cdnLilWolf
09-27-2008, 07:44 AM
Without attempting to insult your intelligence, but I don't know how much you know.

1. Do you have permissions for this directory? Try doing this as root if not.
2. Do the files display with a simple "dir" command?
3. Are they perhaps hidden (preceded with a period, ie; ".DSCN812.jpg").
4. You're aware that Linux is case sensitive, yes?
("DSCN812.jpg" is not the same as "dscn812.jpg")

linuxguru
09-27-2008, 08:40 AM
Try ls dscn*.jpg first to see if any files are present in the directory you are in.
Or ls dscn8*.jpg && ls dscn9*.jpg
At least you will know something exists.

MechanicalMan64
09-27-2008, 08:46 AM
Without attempting to insult your intelligence, but I don't know how much you know.

1. Do you have permissions for this directory? Try doing this as root if not.
2. Do the files display with a simple "dir" command?
3. Are they perhaps hidden (preceded with a period, ie; ".DSCN812.jpg").
4. You're aware that Linux is case sensitive, yes?
("DSCN812.jpg" is not the same as "dscn812.jpg")

i made sure it's at the correct casing, when i simply do ls DSCN812.jpg i can match it, BUT when i do it in range, it doesn't work?

cdnLilWolf
09-27-2008, 08:58 AM
i made sure it's at the correct casing, when i simply do ls DSCN812.jpg i can match it, BUT when i do it in range, it doesn't work?

Care to post a snapshot of your attempts?

In my uploaded sample, I did a similar list.

You are using "square" brackets for your ranges right?

MechanicalMan64
09-27-2008, 09:08 AM
it gives me all the file names, and not the particular range i'm looking for?

cdnLilWolf
09-27-2008, 09:56 AM
Okay, you do understand that this is character matching right? If you want to search by an actual sequence of integers, you'll need to write yourself a little FOR/WHILE script.

MechanicalMan64
09-27-2008, 10:13 AM
ls *DSCN[823-999] -le 957*
ls *DSCN[823-999] -ge 823*

how does this look?

cdnLilWolf
09-27-2008, 10:53 AM
ls *DSCN[823-999] -le 957*
ls *DSCN[823-999] -ge 823*

how does this look?

It looks wrong. Firstly, "e" is not an option within "ls". Secondly, I would suggest googling for some tutorials on BASH shell scripting.

The first asterisk in the command is likely redundant if all the files in question begin with "DSCN". The normal convention for entering options is usually immediately after the command. (ie; "ls -l DSCN[0-9].jpg") The example should return in long-list format ALL files that start with the characters "DSCN", have (any) numeric characters after that and end with the ".jpg" extension.

rob
09-27-2008, 11:54 AM
I've gotten sort of lazy with this sort of stuff.

Unless I'm writing a script to automate a process or if I do something more then once, I'll use excel to do this sort of stuff.

Out of SecureCRT, I'll just copy-and-paste the results of a directory listing. Excel handles this sort of thing particularly well. I'll work with file names... usually if I have a bunch of files to rename, it's hella easier to do this with a few variable substitutions in excel then to wade through the syntax of a new command that I might not use.

This defiantly isn't best practice and if I do anything more then once, I'll force myself to learn the command. However, for one off's and quick conditional renaming, nothing works like good old excel.

MechanicalMan64
09-27-2008, 12:07 PM
if [ $# -ne 1 ]
then
echo "Usage - $0 file-name"
exit 1
fi

if [ -f $1 ]
then
echo "$1 file exist"
else
echo "Sorry, $1 file does not exist"
fi

DazedNConfuzed
10-09-2008, 01:07 AM
if [ $# -ne 1 ]
then
echo "Usage - $0 file-name"
exit 1
fi

if [ -f $1 ]
then
echo "$1 file exist"
else
echo "Sorry, $1 file does not exist"
fi

lol what's the purpose of this script? It doesn't really have any functionality.