Excluding files with timestamp from ls


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Excluding files with timestamp from ls
# 1  
Old 06-11-2015
Excluding files with timestamp from ls

Hi,

I have a list of files all starting with aa but some of them also have a timestamp suffixed which I want to remove from my search. For e.g.

Code:
aa1
aa2
aa.15-05-25_20:41:05.20150611
aa.15-05-26_20:29:40.20150611
aa.15-05-27_20:28:32.20150611


If I do ls -1 aa*, it will list everything but I want the timestamped ones to be excluded from ls. How do I make use of grep -v, if at all I can?Any help would be appreciated
# 2  
Old 06-11-2015
With your sample filenames, you can use something like:
Code:
ls -l aa* | grep -v '[.]'

# 3  
Old 06-11-2015
Code:
ls aa[^.]*

# 4  
Old 06-15-2015
Thank you Don Cragun and Aia for your reply. That's exactly what I was looking for.

@Don Cragun - My actual file list looks like this:

Code:
 sample-test~1.lg.15-05-25_20:41:05.20150611
 sample-test~1.lg.*
 sample-test~1.lg.15-05-27_20:28:32.20150611
 sample-test~1.lg.15-05-26_20:29:40.20150611
 sample-test~1.lg.20160431.lg
 sample-test~1.lg.20150301.lg
 sample-file~df_gp~1.lg.20150616
 sample-test~1.lg.20150616
 sample-test~1.lg.15-05-28_20:28:27.20150616
 sample-test~1.lg.15-05-27_20:28:32.20150616
 sample-test~1.lg.15-05-26_20:29:40.20150616
 sample-test~1.lg.15-05-25_20:41:05.20150616
 sample-bat-test~1.lg.20150616

So, inline with the example given by you, I managed to filter out the ones with timestamp by using ls -1 sample* | grep -v '[:]' and obtained the below:

Code:
sample-bat-test~1.lg.20150616
sample-test~1.lg.*
sample-test~1.lg.20150301.lg
sample-test~1.lg.20150616
sample-test~1.lg.20160431.lg
sample-file~df_gp~1.lg.20150616

I want to create empty files of all the ones without a timestamp so tried doing this but doesn't seem to work:

Code:
for fn in `ls -1 sample* | grep -v '[:]'`
do
touch fn
done

Could you please tell me where I've gone wrong?
# 5  
Old 06-15-2015
Quote:
Originally Posted by swasid
[...]

I want to create empty files of all the ones without a timestamp so tried doing this but doesn't seem to work:

Code:
for fn in `ls -1 sample* | grep -v '[:]'`
do
touch $fn
done

Could you please tell me where I've gone wrong?
Without the `$' what you are doing is creating an empty file named fn and successively updating the metadata each time it loops.

With a `$' you are just updating the metadata of the file that the for loop iterates over.

What you need is a different location where that file can live without conflicting with others, or change to a different name.
# 6  
Old 06-15-2015
As Aia said, in this case, you're just creating or updating the timestamps of a file named fn with your current loop. The touch command creates files named as operands if they didn't already exist and updates the timestamps of files that already existed; it doesn't remove the contents of a file.

If what you're trying to do is turn your selected files into empty files, there is no need for ls or grep, you can just use something like:
Code:
#!/bin/ksh
for fn in sample*
do	[ "$fn" == "${fn%*:*}" ] && > "$fn" && printf '%s truncated\n' "$fn"
done

which (since it just uses shell built-ins and doesn't need to invoke ls, grep, and touch) will run faster, and it will work even if some of your filenames contain whitespace characters (i.e., space, tab, or newline).
# 7  
Old 06-16-2015
Thank you both. Is there a way I can exclude the files with both the date and timestamp? I mean only the below files need to be retained:
Code:
 sample-test~1.lg.
 sample-test~1.lg.
 sample-file~df_gp~1.lg.
 sample-bat-test~1.lg.

from the list below:

Code:
 sample-test~1.lg.15-05-25_20:41:05.20150611
 sample-test~1.lg.15-05-27_20:28:32.20150611
 sample-test~1.lg.15-05-26_20:29:40.20150611
 sample-test~1.lg.
 sample-test~1.lg.
 sample-file~df_gp~1.lg.
 sample-test~1.lg.20150616
 sample-test~1.lg.15-05-28_20:28:27.20150616
 sample-test~1.lg.15-05-27_20:28:32.20150616
 sample-test~1.lg.15-05-26_20:29:40.20150616
 sample-test~1.lg.15-05-25_20:41:05.20150616
 sample-bat-test~1.lg.

meaning any file that has any kind of a date or time extension should get filtered out. ls -1 sample* | grep -v '[:,-]' is removing all of the files because of the hyphen between sample and test.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

2. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

3. Shell Programming and Scripting

Identifying files with a timestamp greater than a given timestamp

I need to be able to identify files with file timestamps greater than a given timestamp. I am using the following solution, although it appears to compare files at the "seconds" granularity and I need it at the milliseconds. When I tested my solution, it missed files that had timestamps... (3 Replies)
Discussion started by: nkm0brm
3 Replies

4. UNIX for Dummies Questions & Answers

Count number of files in directory excluding existing files

Hi, Please let me know how to find out number of files in a directory excluding existing files..The existing file format will be unknown..each time.. Thanks (3 Replies)
Discussion started by: ammu
3 Replies

5. Shell Programming and Scripting

cp -Rp excluding certain files?

Hi, I'm using cp -Rp to copy directories while preserving permissions. I want to exclude any ".lproj" files except for "English.lproj" and "en.lproj" from being copied, but the rest is copied as usual. Is there a way to do this (without using tar or other compression utilities) Thanks (3 Replies)
Discussion started by: pcwiz
3 Replies

6. UNIX for Dummies Questions & Answers

Copy Directories excluding files

Hi guys, I want to copy folder and sub folders only. I don't want the files. If i use cp -r command it will copy entirely with files. Could any one suggest me. Thanks in advance (1 Reply)
Discussion started by: karthik82
1 Replies

7. UNIX for Advanced & Expert Users

listing files excluding files from control file

I have a directory named Project.I have a control file which contains valid list of files.I would like list the files from directory Project which contains files other than listed in the control file. Sample control file: TEST SEND SFFFILE CONTL The directory contains followign... (15 Replies)
Discussion started by: ukatru
15 Replies

8. UNIX for Advanced & Expert Users

find excluding the hidden files

Hi , I am trying to use the find command with delete in a directory . Even though i use a wil character search the find command is checking the hidden files which inturn results in error . Can i avoid look that into the hidden files ?? I am using HP unix . find /cv1/ -name "ite*"... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

9. Shell Programming and Scripting

search for files excluding binary files

Hi All, I need a solution on my following find command find ./.. -name '*.file' -print BTW This gives me the output as belows ./rtlsim/test/ADCONV0/infile/ad0_dagctst.file ./rtlsim/test/ADCONV0/user_command.file ./rtlsim/test/ADCONV0/simv.daidir/scsim.db.dir/scsim.db.file... (2 Replies)
Discussion started by: user_prady
2 Replies

10. Shell Programming and Scripting

Excluding Old Files on AWK !!!

People, I'm sorry because my english is not very good, I'm from Brazil and I need to create shell script that exclude old files. How can I do this? I have an AWK script that works for Unix TRU64 (DIGITAL) and the same script does not work for SUN SOLARIS 5.8. Follows the script fragment: ... (2 Replies)
Discussion started by: alexalvarenga
2 Replies
Login or Register to Ask a Question