Using ls command

Dear All,

I will be having the below files in a directory.

FORM_9757_TYUIOP_1.DAT
FORM_9840_OPRTOP_2.DAT
FORM_9757_TIRTOP_3.DAT

So, from the directory, I have to find only those file which have "OP" at the highlighted position. The file name format is fixed.

Please help :frowning:

Hello,

the wildcard for a single character is ? , so your ls-command would look like this:

$ touch FORM_9757_TYUIOP_1.DAT FORM_9840_OPRTOP_2.DAT FORM_9757_TIRTOP_3.DAT
$ touch XOPX.DAT
$ touch X.DAT
$ ls
FORM_9757_TIRTOP_3.DAT  FORM_9840_OPRTOP_2.DAT  XOPX.DAT
FORM_9757_TYUIOP_1.DAT  X.DAT
$ ls ??????????????OP??????
FORM_9757_TIRTOP_3.DAT  FORM_9757_TYUIOP_1.DAT  FORM_9840_OPRTOP_2.DAT

with command find

find pathOfYourFolder  -name "*OP*"

Instead of * which matches zero or more of any character, you could use ? to indicate a single character, so:-

ls *Hello*.txt
ls ??????Hello???.txt

The first will match any files that has Hello anywhere in the name and finishes with .txt where the second option will only match files that have Hello from character 7, then any three characters and ending .txt

If your current directory has the following files:-

Hello.txt
Hello-me.txt
123Hello.txt
123456Hello123.txt

.... The first command will match all files, but the second will only match the last file.

You can use this as a file name mask for find too, e.g.

find /your/path -name "??????Hello???.txt"

I hope that this helps,
Robin

You could also use the underscores as markers:

ls *_*_*OP_*.DAT

Although a bit less exact, it will probably get you what you want..

The unquoted glob-expression is evaluated by the shell, so you can as well do

echo ????_????_????OP_?????

ls -1 (or ls if not seeing an interactive terminal) puts output line by line, that is sometimes convenient. Like the following that pipes the output to grep

ls -1 | grep '^...._...._....OP_.....$'

grep uses Regular Expression: . is a single character, ^ is line start, $ is line end.

ls FORM_[0-9][0-9][0-9][0-9]_[A-Z][A-Z][A-Z][A-Z]OP_?.DAT

or

ls FORM_[0-9][0-9][0-9][0-9]_[A-Z][A-Z][A-Z][A-Z]OP_[0-9].DAT

Hello Guys,
Thanks a lot for your help.
However I have a new question now.
My file will be of type

FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT
FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT.SKIP
FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT.ERROR

I have got the answer of how to pick up FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT . But now the problem is DAT can be TXT and there can be lot of files . Like,

FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT
FORM_PTIR_9820_ANFDOP_20150716_00413561.TXT
FORM_PTIR_9820_ANFDOP_20150716_00413562.DAT
FORM_PTIR_9820_ANFDOP_20150716_00413563.DAT
FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT.SKIP
FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT.ERROR

So,
I cannot use ????_????_????_????OP_*.??? to transfer all files with OP in middle and ending with .DAT or .TXT to other folder.

????_????_????_????OP_*.???* will pick up .ERROR and .SKIP files as well, so how do I skip the .ERROR and .SKIP files and transfer rest to other folder ?

Please help

I don't understand.

$ ls -1
FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT
FORM_PTIR_9820_ANFDOP_20150716_00413561.TXT
FORM_PTIR_9820_ANFDOP_20150716_00413562.DAT
FORM_PTIR_9820_ANFDOP_20150716_00413563.DAT
FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT.SKIP
FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT.ERROR
$ ls -1 ????_????_????_????OP_*.???
FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT
FORM_PTIR_9820_ANFDOP_20150716_00413561.TXT
FORM_PTIR_9820_ANFDOP_20150716_00413562.DAT
FORM_PTIR_9820_ANFDOP_20150716_00413563.DAT

Aren't these the expected ones?
If yes you can copy/move them to other folder:

cp  ????_????_????_????OP_*.???  /other/folder/

Dear Friends,

Thanks. maybe my previous post was not up to the mark to explain what is required.

My current directory is like, below
----------------------------

FORM_PTIR_9820_ANFDOP_20150716_00413562.DAT_bkp
FORM_PTIR_9820_ANFDOP_20150716_00413563.DAT_bkp
FORM_PTIR_9820_ANFDEX_20150716_00413562.DAT
FORM_PTIR_9820_ANFDEX_20150716_00413563.DAT
FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT

-------------------------------
Now with ????_????_????_????OP*.??? I will get one file (the last one in above list). How to get a negation of that ? I mean how to list all other files except for these ones which have been derived using the mentioned wildcard ? Please help :frowning: I tried -I, did not work.

something like

ls *|grep -v "$(ls ????_????_????_????OP_*.???)"

but this is getting ridiculous, why not say exactly what you want : All the requisites and give ALL the information?

Thanks buddy ! Honestly I know this is getting messy. I would let you know as soon as I have the complete info. Till now, I have asked what I knew of.

---------- Post updated at 04:59 AM ---------- Previous update was at 04:42 AM ----------

Ok guys, this is my exact requirement.

My current working directory.

-rwxrwxr-x 1 dev115 devt 299884 Aug 13 15:09 FORM_PTIR_9820_ANFDOP_20150716_00413562.DAT_bkp
-rwxrwxr-x 1 dev115 devt 299884 Aug 13 15:16 FORM_PTIR_9820_ANFDOP_20150716_00413563.DAT_bkp
-rwxrwxr-x 1 dev115 devt 299884 Aug 13 16:36 FORM_PTIR_9820_ANFDEX_20150716_00413562.DAT
-rwxrwxr-x 1 dev115 devt 299884 Aug 13 16:36 FORM_PTIR_9820_ANFDEX_20150716_00413563.DAT
drwxr-xr-x 2 dev115 devt   4096 Aug 14 09:15 NOTProcessed
-rw-rw-r-- 1 dev115 devt     75 Aug 14 09:15 notify.lst
drwxr-xr-x 2 dev115 devt   4096 Aug 14 09:15 Skipped
drwxr-xr-x 2 dev115 devt   4096 Aug 14 09:15 Processed
drwxr-xr-x 2 dev115 devt   4096 Aug 14 09:15 Error
-rwxrwxr-x 1 dev115 devt 299884 Aug 14 10:15 FORM_PTIR_9820_ANFDOP_20150716_00413561.DAT

=====================================

now I have to move the files which are excluded from the wildcard ????_????_????OP*.??? to a folder called NOTProcessed.

list will give me a set of files which I will have to write in a txt file and then move them one by one.

How can we achieve move with this ?

Say,

  ls -ltr | grep -v  ????_????_????_????OP*.??? | grep -v ^d

gives me set of all files excluding directory (in any). Now I want to move these files(result set from ls command) to folder NOTProcessed. How can we achieve this ? I want to avoid file names to be written in a txt file and then loop over these names and put one by one. Can we move this in a single command ? Kindly help ! I need it badly...

Try this one

for i in *
do
  if [[ -f "$i" ]] && [[ "$i" != ????_????_????_????OP_*.??? ]]
  then
    echo mv "$i" NOTProcessed/
  fi
done

Instead of * you can use e.g. FORM* do reduce the scope.
Remove the echo to activate.

With a recent bash , for a selection inversion, try

shopt -s extglob
ls !(????_????_????_????OP*.???)