How to use ls with pattern and including path?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use ls with pattern and including path?
# 1  
Old 08-20-2014
How to use ls with pattern and including path?

Hello to all,

Maybe someone could help me, my question is:

How can a filter the print of command ls for the files with names of the form "abc*.txt" including the path?

I've done this:

If I move with command cd to /My/Path/Is/This/ and send this command:
Code:
ls -lst abc*.txt -i ?zabc*.txt

I get a filtered list containing files with names only with pattern abc*.txt and it seems to work fine.

But if I try to use the same command from another directory and including the path, the filtering is not working.

I've tried like this:
Code:
ls -lst abc*.txt -i ?zabc*.txt /My/Path/Is/This/

and like this and doesn't apply the filter neither
Code:
ls /My/Path/Is/This/ -lst abc*.txt -i ?zabc*.txt

Thanks for any help.
# 2  
Old 08-20-2014
* is not a feature of ls, * is something the shell does.

So when you do ?zabc*.txt in the current directory, the shell will parse that into a list of names to feed into ls -- which happens before ls even runs.

The shell will let you mash them together though. It'll even let you quote any parts of it which don't have *'s and stuff in them:

Code:
ls "/My/Path/Is/This/"?zabc*.txt

If all you need is the names, you don't need ls -- remember the shell does it, not ls.

Code:
for FILENAME in "/My/Path/Is/This/"?zabc*.txt
do
        echo "Got file name $FILENAME"
done

# 3  
Old 08-20-2014
Hello Corona688,
Thanks for your help. Actually I need the filename, date, hour, size etc, like what "ls -lt" gives.
I've modified your code to:
Code:
for FILENAME in "/My/Path/Is/This/"?zabc*.txt
do
        ls -lt "$FILENAME"
done

The only thing is that is printing the file with the path like this:
Code:
-rw-r--r-- 1 ri xyz  1048 Aug 11 05:15 /My/Path/Is/This/abc-dfe.txt

How to print the file without the path?, like this:
Code:
-rw-r--r-- 1 ri xyz  1048 Aug 11 05:15 abc-dfe.txt

Thanks again.

Best regards
# 4  
Old 08-20-2014
You don't need the for loop if you're feeding it into ls, use my other example.

Quote:
Originally Posted by Ophiuchus
The only thing is that is printing the file with the path like this:
Code:
-rw-r--r-- 1 ri xyz  1048 Aug 11 05:15 /My/Path/Is/This/abc-dfe.txt

How to print the file without the path?, like this:
Code:
-rw-r--r-- 1 ri xyz  1048 Aug 11 05:15 abc-dfe.txt

Thanks again.

Best regards
Code:
cd /path/to/folder ; ls *.whatever

# 5  
Old 08-20-2014
Thank you Corona,

It works like this:
Code:
cd /path/to/folder ; ls *.whatever; cd-

The last issue it seems to be that if I use the pattern abc*.txt I get filenames like 123abc-yue.txt or 01xabc-oie.txt.

How to get only files that begin with "abc"?

Thanks again

Last edited by Ophiuchus; 08-20-2014 at 04:41 PM..
# 6  
Old 08-20-2014
You don't, unless you want to put the output through a text filter to get rid of it.
# 7  
Old 08-20-2014
Thank you Corona,

It works like this:

Code:
cd /path/to/folder ; ls *.whatever; cd-


The last issue it seems to be that if I use the pattern abc*.txt I get filenames like 123abc-yue.txt or 01xabc-oie.txt and I would like only those that begins with abc.

How to get only files that begin with "abc"? It could be done modifying the pattern only?

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete a pattern including any whitespace before it and after it

Hello. A_LIGNE="cnezJ,V ,FMZ fd,Mok CODKJ,F SOME_WORD fcnQ, VMQKV Q" A_PATTERN="SOME_WORD" sed 's/'$A_PATTERN'//g' <<< "$A_LINE"will remove 'SOME_WORD' and give : "cnezJ,V ,FMZ fd,Mok CODKJ,F fcnQ, VMQKV Q"A_PATTERN="SOME_WORD]" sed 's/'$A_PATTERN'//g' <<< "$A_LINE"will remove... (4 Replies)
Discussion started by: jcdole
4 Replies

2. Shell Programming and Scripting

Comment all lines which are not already commented for each full path pattern matched

Hello. Question 1 : I want to comment out all lines of a cron file which are not already commented out for each full path pattern matched. Example 1 nothing to do because line is already commented out; pattern = '/usr/bin/munin-cron' # */5 * * * * munin test -x... (3 Replies)
Discussion started by: jcdole
3 Replies

3. Shell Programming and Scripting

Find and replace the path value in files, pattern is not full known.

Hi, I need to do find and replace, but the pattern is not full known. for example, my file has /proj/app-d1/sun or /data/site-d1/conf here app-d1 and site-d1 is not constant. It may be different in different files. common part is /proj/xx/sun and /data/xxx/conf i want to find where ever... (6 Replies)
Discussion started by: rbalaj16
6 Replies

4. Shell Programming and Scripting

Pattern match a path anywhere in the line and replace it with new path

I want to pattern match only path part from below and replace them with new path string. LoadModule jk_module /fldrA/fldrBaf/fldrCaa/modules/mod_jk.so JkWorkersFile /fldrA/fldrBaf/fldrCaa/config/OHS/ohs1/workers.properties JkLogFile... (4 Replies)
Discussion started by: kchinnam
4 Replies

5. Shell Programming and Scripting

Including EOL in egrep pattern for multiple lines

Hi all I need your help to get a high-performance solution. I am working on a extensive script to automate file restores using the bprestore tool on a Solaris 5.10 server (bash 3.00). I will only paste the needed parts of the script to avoid any confusion. To use the script the user has to... (2 Replies)
Discussion started by: Anonym
2 Replies

6. Shell Programming and Scripting

Find file and zip without including directory path

Does anyone know of a way to zip the resulting file from a find command? My approach below finds the file and zips the entire directory path, which is not what I need. After scanning the web, it seems to be much easier to perform gzip, but unfortunately the approach must use zip. find `$DIR`... (5 Replies)
Discussion started by: koeji
5 Replies

7. Shell Programming and Scripting

remove contents including the tag if pattern matches

Hi all, Can anyone help me on this. I have several WP sites that are affected by sql injections. But the contents are different as follows western union india belgaum western union india bolegaon western union india barhaj western union india budhana western union india belda western... (6 Replies)
Discussion started by: sanjuabraham
6 Replies

8. Shell Programming and Scripting

Search for Pattern and Print including Lines in between

Gurus, I have a big file that needs to be sorted out and I cant figure out what to do. The file name is as below: Name: xxxx yyyy nnnn Description: dfffgs sdgsgsf hsfhhs afgghhjdgj fjklllll gsfhfh Updated: jafgadsgg gsg Corrected: date today The file consists of line like these. ... (13 Replies)
Discussion started by: The One
13 Replies

9. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

10. Shell Programming and Scripting

Reading a path (including ref to shell variable) from file

Hi! 1. I have a parameter file containing path to log files. For this example both paths are the same, one is stated directly and the second using env variables. /oracle/admin/orcl/bdump/:atlas:trc:N ${ORACLE_BASE}/admin/${ORACLE_SID}/bdump/:${ORACLE_SID}:trc:N 2. I try to parse the path... (1 Reply)
Discussion started by: lojzev
1 Replies
Login or Register to Ask a Question