awk help with find command and filenames with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk help with find command and filenames with spaces
# 1  
Old 10-05-2009
awk help with find command and filenames with spaces

I have the following code:

Code:
 find /usr/local/test5 -type f -mtime +30 -exec ls -l {} \; | awk '{print $5, $6, $7, $8, $9}'

I have this as output:

Code:
14 Aug 12 00:00 /usr/local/test5/file1
14 Aug 12 00:00 /usr/local/test5/lastname,

The bolded part is where I run into trouble. The actual filename is:

Code:
/usr/local/test5/lastname, firstname.pdf

I want to return the whole file name with spaces and all and delete it at the end of the find command via | and xargs.

Anyone know how to handle issues like this with spaces in the filenames? Keep in mind this is a script that deals with files uploaded by users I have no control over, so spaces in filenames I can't control.
# 2  
Old 10-05-2009
Code:
find /usr/local/test5 -type f -mtime +30 -exec ls -l {} \; | cut -d " " -f 6-

# 3  
Old 10-05-2009
Code:
find /usr/local/test5 -type f -mtime +30 -print | while read FILENAME
do
          # The filename including spaces is now in ${FILENAME}
          echo "${FILENAME}"
done

# 4  
Old 10-05-2009
Quote:
Originally Posted by protocomm
Code:
find /usr/local/test5 -type f -mtime +30 -exec ls -l {} \; | cut -d " " -f 6-

Quote:
Originally Posted by methyl
Code:
find /usr/local/test5 -type f -mtime +30 -print | while read FILENAME
do
          # The filename including spaces is now in ${FILENAME}
          echo "${FILENAME}"
done

Hey guys thanks for this, I am trying to add another line to delete the output with "rm" , but I am getting errors saying "no such file or directory"
this is obviously due to the spaces in the filename.

Here is an example of what I am running that is throwing errors.

Code:
find /usr/local/test5 -type f -mtime +30 -exec ls -l {} \; | cut -d " " -f 6- | grep -- "lastname, firstname.pdf" | xargs rm


Last edited by streetfighter2; 10-05-2009 at 05:45 PM..
# 5  
Old 10-06-2009
Quote:
Originally Posted by streetfighter2
Hey guys thanks for this, I am trying to add another line to delete the output with "rm" , but I am getting errors saying "no such file or directory"
this is obviously due to the spaces in the filename.

Here is an example of what I am running that is throwing errors.

Code:
find /usr/local/test5 -type f -mtime +30 -exec ls -l {} \; | cut -d " " -f 6- | grep -- "lastname, firstname.pdf" | xargs rm

Hi!

Well, the space character is lurking there again when running xargs. You should loop over the file names as methyl proposed earlier.

pen
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handling filenames with spaces

I'm trying to handle some files with spaces in their name using "" or \ . Like "file 1" or file\ 1. My current confusion can be expressed by the following shell script: #!/bin/bash touch "file 1" "file 2" echo -n "ls: " ; ls echo --- for file in "file 1" "file 2" ; do echo $file... (9 Replies)
Discussion started by: Ralph
9 Replies

2. UNIX for Dummies Questions & Answers

Reading filenames with spaces

Hello I've got a certain no. of files in a directory whose names I'm reading and redirecting into a temporary text file using the command below: ls -l | grep ^- | awk '{print $9}'However, whenever the file names contain spaces the above command considers only the part of the file name up to... (5 Replies)
Discussion started by: S. BASU
5 Replies

3. Shell Programming and Scripting

awk and spaces in filenames

Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file: ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
Discussion started by: rodan90
3 Replies

4. Shell Programming and Scripting

Print filenames with spaces using awk

Hello all, I want to list the file contents of the directory and number them. I am using la and awk to do it now, #ls |awk '{print NR "." $1}' 1. alpha 2. beta 3. gamma The problem I have is that some files might also have some spaces in the filenames. #ls alpha beta gamma ... (7 Replies)
Discussion started by: grajp002
7 Replies

5. Solaris

feeding filenames to find command

Hi, I am having set of files whose names are stored in a file say "filelist.txt" Now, I want to find all files contained in "filelist.txt" from my parent directory. Is there any way to let find command understand "filelist.txt" just like we have option -f in awk. I donot want to run a... (4 Replies)
Discussion started by: sanjay1979
4 Replies

6. Shell Programming and Scripting

Moving filenames containing spaces

I want to ftp all the sh files in the directory. Also if any of the file name contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below: FILESSH=$(ls /mysh/*.sh) --- FILESH being used here for some other task --- echo "$FILESSH" |... (3 Replies)
Discussion started by: amicon007
3 Replies

7. Shell Programming and Scripting

spaces in filenames

Hi I hope someone will be able to resolve this little teaser! I am running a script for file in `ls directory` do echo "$file" ...other code here.... done this works fine unless we receive a file with a name which has a space in it ie "filena me" (I know its not good... (8 Replies)
Discussion started by: Bab00shka
8 Replies

8. Shell Programming and Scripting

spaces in filenames, for do

Hi All, I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory. What I have: ... (4 Replies)
Discussion started by: naviztirf
4 Replies

9. Shell Programming and Scripting

Unix filenames and spaces

I have files on my unix boxes that users have created with spaces. Example: /tmp/project plan ls -l "/tmp/project plan" works fine. $/tmp>ls -l "/tmp/project plan" -rw-r--r-- 1 root other 0 Jan 31 12:32 /tmp/project plan I created a file called test and put just the... (2 Replies)
Discussion started by: x96riley3
2 Replies

10. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies
Login or Register to Ask a Question