2

I'm writing a bash script which in a certain part of the process should list the files in a directory older than 1 day and print the list to a text file to work with it later. This is the current command I have:

find . -mtime +0 > list.txt

The problem with this command is that it prints the filenames preceded by "./", e.g.:

./file1
./file2
./file3

How can I do to print only the filenames in this way?

file1
file2
file3

2 Answers 2

3

Use basename:

find . -mtime +0 -type f -exec basename {} \; > list.txt

(the reason for the -type f is because otherwise the searched directory is printed).

Sign up to request clarification or add additional context in comments.

3 Comments

@mklement0 Accidental compliance... :)
I appreciate the confession :)
@mklement0 Maybe I'm just naturally POSIX-compliant, without even realising :D
1

No need to use extra binary commands if your find supports it:

find . -mtime +0 -printf '%f\n' > list.txt

When targeting files, just add -type f:

find . -mtime +0 -printf '%f\n' -type f > list.txt

Or if you intend to show the files and directories in a specified directory:

find some_dir -mtime +0 -printf '%f\n' -mindepth 1 > list.txt

5 Comments

I like this best, but you also need -type f for same reason I did.
+1; the best solution, IF -printf is supported (it is not part of POSIX). GNU find does support it. Others?
Is it -type f really necessary? I tried the first command without that argument and it printed only the filenames and not the directory.
@ManueldelaFuente Perhaps if you don't have directories in it or that you don't need to exclude them.
@konsolebox Well, I thought what @trojanfoe said in the other answer was that to run the command without type would also print the parent directory, which I found odd, though maybe I misunderstood. Anyway, all answers help me but I'll take this one, thank you so much. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.