LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-09-2008, 06:46 PM   #1
yanchina12
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Rep: Reputation: 0
seraching folders for particular file


Dear Experts,

I've several folder with name like u001, u002
and so on and in each folder for example in u001, there are files with u001.tc u001.cm etc based on the folder name (in u002 there will be u002.tc, u002.cm ). I'm trying to have a code that will search the folders for corresponding .cm files (u001.cm and u002.cm and so one) and list all the folders numbers (like u001 etc)printed in a text file that has not got any .cm file.

I'm not a computer expert, so any scripts will be highly appreciated!

Regards,
Yan
 
Old 07-09-2008, 07:06 PM   #2
sdexp
Member
 
Registered: Sep 2003
Location: USA
Distribution: Ubuntu Linux
Posts: 103

Rep: Reputation: 15
This looks like a close representation of a script that you are looking for:

Code:
locate .cm | grep "u[0-9]" | sed "s/\/ /g" | awk "{ print $2; }"
Feel free to tinker with it.
 
Old 07-09-2008, 07:08 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Are the directories all at the same level? If so, you can simply do
Code:
for dir in u???
do
  ls $dir/*.cm > /dev/null 2>&1 || echo $dir >> text_file
done
the || sign executes the echo command whenever ls fails, that is every time there isn't a .cm file in that folder. The standard error and standard output of ls are sent to the black hole.
 
Old 07-09-2008, 07:13 PM   #4
hubbruch
LQ Newbie
 
Registered: Apr 2007
Location: Europe
Distribution: Slackware
Posts: 14

Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Are the directories all at the same level? If so, you can simply do
Code:
for dir in u???
do
  ls $dir/*.cm > /dev/null 2>&1 || echo $dir >> text_file
done
the || sign executes the echo command whenever ls fails, that is every time there isn't a .cm file in that folder. The standard error and standard output of ls are sent to the black hole.

Dude, top notch.
 
Old 07-10-2008, 08:12 AM   #5
yanchina12
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Many thanks

Quote:
Originally Posted by colucix View Post
Are the directories all at the same level? If so, you can simply do
Code:
for dir in u???
do
  ls $dir/*.cm > /dev/null 2>&1 || echo $dir >> text_file
done
the || sign executes the echo command whenever ls fails, that is every time there isn't a .cm file in that folder. The standard error and standard output of ls are sent to the black hole.
Dear Sir,

Many thanks for your help. I'll try. Are the directories all at the same level? If so, you can simply do

I have a folder calles 777 and within this folder there are 1000 folders (u001, u002 and so on). Does it answer the question?
 
Old 07-10-2008, 08:34 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by yanchina12 View Post
I have a folder calles 777 and within this folder there are 1000 folders (u001, u002 and so on). Does it answer the question?
Yes. Go into directory 777 and execute the code. It will look for directories from u001 to u999, since the u??? syntax stats for u followed by three characters.

Note that if you have other directories whose name is 4 characters long, beginnining with u, they match as well (e.g. uabc, ubcd, ua01, and so on).
 
Old 07-10-2008, 08:39 AM   #7
yanchina12
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Many thanks

Quote:
Originally Posted by hubbruch View Post
Dude, top notch.
Dear Sir,

Many thanks for your help. I'll try!

In regards to the question "Are the directories all at the same level?" If so, you can simply do

I have a folder calles 777 and within this folder there are 1000 sub-folders (u001, u002 and so on) and I'm try to get the information for each subfolders.

The whole goal is to find out the name of the subfolders has not got the corresponding .cm files. We must remember that on each folders there are other files.

I'm not an expert user so your help is highly appreciated!
 
Old 07-10-2008, 08:54 AM   #8
yanchina12
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Yes. Go into directory 777 and execute the code. It will look for directories from u001 to u999, since the u??? syntax stats for u followed by three characters.

Note that if you have other directories whose name is 4 characters long, beginnining with u, they match as well (e.g. uabc, ubcd, ua01, and so on).
Dear Sir,

If the if the directories are u00000 (five digits), Do I need to use five? sign.

Regards,
S
 
Old 07-10-2008, 11:41 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by yanchina12 View Post
If the if the directories are u00000 (five digits), Do I need to use five? sign.
Yes. But only the u00000 directories will match, not u000 or u0000. If you have different formats for the directories names, and no other file or directory start with character "u", you can use u* instead of u???. This will match ANY number of character after "u".

Otherwise you have to use a counter to loop over all the directories or a regular expression. Which is the exact sequence of your directories, anyway? The first is u001... and the last one?
 
Old 07-10-2008, 12:34 PM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
try
Code:
find . -name *.cm
note: season to taste
 
Old 02-18-2009, 07:07 AM   #11
yanchina12
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
`do^M' unexpected error

Dear All,

I'm trying to run the following script and getting `do^M' unexpected error. Please help. I'm very new to the Linux. many thanks.


#!/bin/bash
for dir in u*
do
ls $dir/*.cm > /dev/null 2>&1 || echo $dir >> text_file
done
printf "DONE\n"
 
Old 02-18-2009, 08:28 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
It looks like you have written the script under windows, so it retains the line feed terminator which is not valid in linux and is not managed by the shell. If this is the case, just run the command
Code:
dos2unix script.sh
where script.sh is the actual name of your script. This will remove the foreign control character, like ^M from the file.
 
Old 02-18-2009, 08:51 AM   #13
bernihard
LQ Newbie
 
Registered: Feb 2009
Location: Spain
Distribution: CentOS 5.2
Posts: 13

Rep: Reputation: 2
Quote:
Originally Posted by colucix View Post
It looks like you have written the script under windows, so it retains the line feed terminator which is not valid in linux and is not managed by the shell. If this is the case, just run the command
Code:
dos2unix script.sh
where script.sh is the actual name of your script. This will remove the foreign control character, like ^M from the file.
Also you can try:
Code:
tr -d '\r' < script.sh > new_script.sh
When moving text files from windows
 
Old 02-25-2009, 11:21 AM   #14
yanchina12
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Please help

Dear All,

I've been trying to run the following code

for dir in u*
do
ls $dir/*.cm > /dev/null 2>&1 || echo $dir >> text_file
done

It runs without any error but without any text file with the folder names.

If you please help, would be great!

Regards,
Yan
 
Old 02-25-2009, 11:38 AM   #15
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
Quote:
Originally Posted by yanchina12 View Post
Dear All,

I've been trying to run the following code

for dir in u*
do
ls $dir/*.cm > /dev/null 2>&1 || echo $dir >> text_file
done

It runs without any error but without any text file with the folder names.

If you please help, would be great!

Regards,
Yan
i think the || is an or so bash will either do ls $dir/*.cm > /dev/null 2>&1 (which prints doesnt print anything) or it does echo $dir >> text_file; it chooses to do the first one each time.

try:
Code:
for dir in u*
do
  ls $dir/*.cm > /dev/null 2>&1 && echo $dir >> text_file
done
__________________

Code:
find . -name "*.cm"
will list dirs that contain a *.cm file.

__________________

if you are only interesed in dirs that dont contain a *.cm file; i came up with this [untested]:
Code:
for dir in u*
do
 if [ ! -f $dir/*.cm ]
 then
  echo $dir # >> append_to_some_text_file
 fi
done

Last edited by schneidz; 02-25-2009 at 11:53 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Change file permissions of files in folders dotancohen Linux - Newbie 1 05-19-2007 03:56 AM
Folders, and Mounts and File associations in KDE wombat53 Slackware 9 10-02-2005 11:18 PM
Desktop File Folders don't work Keithj Linux - Software 1 06-14-2005 01:25 PM
File moving on mounted folders from Java zeshanzia Programming 0 03-11-2005 02:00 AM
Changing file permissions within multiple folders cloudxiii Linux - Newbie 1 02-21-2005 01:20 AM


All times are GMT -5. The time now is 02:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration