Checking if the files in a directory have a txt extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking if the files in a directory have a txt extension
# 8  
Old 05-06-2010
in bash:
Code:
for file in $dir1/* ; do
if [[ "$file" =~ ".txt" ]]; then
echo "Skipping $file (is a txt file)"
fi
done

# 9  
Old 05-06-2010
Code:
for f in $(find . -name "*.txt"); do
    echo Do something with ${f}.
done


Last edited by dunkar70; 05-06-2010 at 01:59 PM..
# 10  
Old 05-06-2010
Quote:
Originally Posted by itkamaraj
Code:
for i in `ls *.txt`; do 
echo "$i is text file";  ## do what ever you need here.. $i is the file name (txt file)
done

Never, ever do that. 'for i in *.txt' is safer and cheaper. Using ls and command substitution means that it's impossible to handle filenames with IFS characters (by default, spaces, tabs, and newlines). Also, if there are very many *.txt files in the directory, a command line length limit imposed by the exec system call may prevent the operating system from successfully launching ls; this is not an issue with the shell pattern glob as it does not require a new process.

Please pass along the cluestick thwack when you see someone else doing the same Smilie

Regards,
Alister
# 11  
Old 05-06-2010
Alister is correct that command substitution is expensive in terms of processing resources, but using a simple *.txt will not find the files in sub-directories. See my previous post. Unless you are dealing with thousands of files, the resource constraint should not be a problems.

Last edited by dunkar70; 05-06-2010 at 02:28 PM..
# 12  
Old 05-06-2010
While I did mention that the sh glob is "cheaper" (which I now regret), please note that my post is about correctness and robustness, not performance.

I wasn't referring to searching subdirectories, only that specific use of ls. It serves absolutely no purpose whatsoever and can only result in mishandled filenames. In such a case, where ls is being used to retrieve the filenames in directories (non-recursively), always use file expansion instead.

While we're here, your find solution is also unable to properly handle any filenames with IFS characters (though it's not constrained by a command length limit if there are very many *.txt files). It could be made to handle spaces and tabs correctly, though not newlines, if instead of using the find in a command substitution in a for loop's list, you piped the output of find into a while read loop.

One last point: Please don't misinterpret my intention. I realize that most of the time technically-limited solutions are more than adequate; I use them all the time myself. I simply highlight the limitations so that anyone who is not aware of them will learn.

Regards,
Alister
# 13  
Old 05-06-2010
@alister, Understood.

The statement I provided should handle special characters in the find and for loop statements. How the files are processed after finding them is up to the code within the for loop. File names should not include new line characters...at least I have yet to encounter one. If you have a suggestion for dealing with such an occurrence, please include it so we can all learn from it.

Thanks.
# 14  
Old 05-06-2010
Quote:
Originally Posted by dunkar70
Code:
for f in $(find . -name "*.txt"); do
    echo Do something with ${f}.
done

im trying this in csh but it doesn't seem to work.

btw
im checking the second parameter, like this

if($argv[2] == "-r") then

search in all the sub-folders of that folder

endif
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save files in directory as txt

wget -x -i link.txt The above downloads and create unique entries for the 97 links in the text file. However, each new file is saved as CM080 with a FILE extention. Is there a way to convert each file in that directory to a .txt? The 97 files are in... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

Delete all files with specific extension in directory tree

I'm sure this has been asked many times, but a search didn't turn up a definitive best method for this (if there ever is such a thing). I have been using rsync to back up my main data directory, but I have accumulated a large number of older backups that I don't need. All of the files I don't... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

3. Shell Programming and Scripting

List files with *.i extension in a directory and all its subdirectories + 30days old then remove

I need to write a script to : list files with *.i extension in a directory and all its subdirectories + 30days old, save it in a file and then remove (2 Replies)
Discussion started by: lena keung
2 Replies

4. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

5. Shell Programming and Scripting

[Solved] Giving files .txt extension

Hi there, I have around 145,000 files with no file extension in this directory - /home/adams/29: The file name varies but all end with a number from 0 - 9, e.g. TTFILE_BAT_235496, CCNHATA_RFC_23455 I want to give all these 145,000 .txt extension. Please how do I do that? Thanks (2 Replies)
Discussion started by: Creems
2 Replies

6. UNIX for Dummies Questions & Answers

Copy files with same name but different extension from 2 different directory

Hi all, i have 2 directory of files, the first directory(ext1directory) contain files of extension .ext1 and the second directory(allextdirectory) contains files of multiple extensions (.ext1,.ext2,.ext3,..) so i want to copy the files from directory 2(allextdirectory) that have the same name... (8 Replies)
Discussion started by: shelladdict
8 Replies

7. UNIX for Dummies Questions & Answers

rusty with cp command -- how to cp all .doc files with .txt extension

I'm rusty with cp, so I was wondering: is it possible to cp all the .doc files in a folder and make them .txt files? Can you use cp to do that? (3 Replies)
Discussion started by: Straitsfan
3 Replies

8. Shell Programming and Scripting

copy files with new extension in same directory

I've been able to find all the extensionless files named photos using the command: find /usr/local/apache/htdocs -name photos -print0 I need to copy those files to the name photos.php in their same directory. I've found a bunch of xarg examples for moving to other directories but I wasn't... (7 Replies)
Discussion started by: dheian
7 Replies

9. UNIX for Dummies Questions & Answers

List all files except *.txt in a directory

I have many types of files (Eg: *.log, *.rpt, *.txt, *.dat) in a directory. I want to display all file types except *.txt. What is the command to display all files except "*.txt" (9 Replies)
Discussion started by: apsprabhu
9 Replies

10. UNIX for Dummies Questions & Answers

Checking files extension

Hi, I need one line command to display all files that ends with .scr. Example: In a directory I have 10 files, out of that 4 files have filetype extension .dat and 4 files with .scr and 2 files with .txt.... In this i want to display only files that ends with .scr. I tried some commands,... (2 Replies)
Discussion started by: gwgreen1
2 Replies
Login or Register to Ask a Question