How do I find all the files that were created today in Unix/Linux? - linux

How do I find all the files that were create only today and not in 24 hour period in unix/linux

On my Fedora 10 system, with findutils-4.4.0-1.fc10.i386:
find <path> -daystart -ctime 0 -print
The -daystart flag tells it to calculate from the start of today instead of from 24 hours ago.
Note however that this will actually list files created or modified in the last day. find has no options that look at the true creation date of the file.

find . -mtime -1 -type f -print

To find all files that are modified today only (since start of day only, i.e. 12 am), in current directory and its sub-directories:
touch -t `date +%m%d0000` /tmp/$$
find . -type f -newer /tmp/$$
rm /tmp/$$
Source

I use this with some frequency:
$ ls -altrh --time-style=+%D | grep $(date +%D)

After going through many posts I found the best one that really works
find $file_path -type f -name "*.txt" -mtime -1 -printf "%f\n"
This prints only the file name like
abc.txt not the /path/tofolder/abc.txt
Also also play around or customize with -mtime -1

This worked for me. Lists the files created on May 30 in the current directory.
ls -lt | grep 'May 30'

Use ls or find to have all the files that were created today.
Using ls : ls -ltr | grep "$(date '+%b %e')"
Using find : cd $YOUR_DIRECTORY; find . -ls 2>/dev/null| grep "$(date '+%b %e')"

find ./ -maxdepth 1 -type f -execdir basename '{}' ';' | grep `date +'%Y%m%d'`

You can use find and ls to accomplish with this:
find . -type f -exec ls -l {} \; | egrep "Aug 26";
It will find all files in this directory, display useful informations (-l) and filter the lines with some date you want... It may be a little bit slow, but still useful in some cases.

Just keep in mind there are 2 spaces between Aug and 26. Other wise your find command will not work.
find . -type f -exec ls -l {} \; | egrep "Aug 26";

If you're did something like accidentally rsync'd to the wrong directory, the above suggestions work to find new files, but for me, the easiest was connecting with an SFTP client like Transmit then ordering by date and deleting.

To get file before 24 hours execute below command:
find . -type f -mtime 1 -exec ls -l {} \;
To get files created today execute below command:
find . -type f -mtime -1 -exec ls -l {} \;
To Get files created before n days before, where +2 is before 2 days files in below command:
find . -type f -mtime +2 -exec ls -l {} \;

Related

Combining a few " find "commands in linux

find /home/imk-prac/ \( -type f -size -13c -name '*\?plik\?*' \) -o\( -type d -name '\[Kolo1\]*' \)2> /dev/nul;
This command counts normal files which has less than 13 symbols and contains a sequence of symbols ?plik?.
I want to add looking for files which were modified less than 30 days and I wrote this command:
find /home/imk-prac/ -type f -mtime -30 -exec ls -l {} \; > /dev/null
I don't know how to combine this two commands in to one.
I wanted to add looking for files with specified quantity of symbols and I found this command:
grep -Po '(^|\s)\S{64}(\s|$)' file
But there is the same problem or even worse, because of grep command.
Thanks for your time and I hope you will help me to figure it out ;)

How to use find command with sorting by creation date&time recursively?

I have many time tried for this issue.
find command results do not care listing order.
How to use directory listing based on creation date&time from find recursive command result ?
ls -lc
This works good , but not recursively way .
find . -type f -iname "*.txt" -exec ls -lc {} \;
This doesn’t work.
find . -type f -iname "*.txt" | sort -n
This also only name based.
solution from https://superuser.com/q/294161/992527 combined with the command from the question:
find . -type f -iname "*.txt" -printf "%T# %Tc %p\n" | sort -n
See the different answers for variations of the command.
Explanation cited from referenced question:
printf arguments from man find:
%Tk: File's last modification time in the format specified by k.
#: seconds since Jan. 1, 1970, 00:00 GMT, with fractional part.
c: locale's date and time (Sat Nov 04 12:02:33 EST 1989).
%p: File's name.
Solution for MacOS copied from #cooljobs' comment to the question
find . -type f -iname "*.txt" -exec stat -f '%B %m %N' {} \; | rev | cut -d '/' -f 1 | rev | sort -n

Copy files in Unix generated in 24 hours

I am trying to copy files which are generated in the past one day (24 hrs). I am told to use awk command but I couldn't find the exact command for doing this. My task is to copy files from /source/path --> /destination/path.
find /source/path -type f -mmin -60 -exec ls -al {} \;
I have used the above command to find the list of files generated in the past 60 mins, but my requirement is to copy the files, and not just knowing the file names.
Just go ahead an exec cp instead of ls:
find /source/path -type f -mmin -60 -exec cp {} /destination/path \;
You are really close! Take the name of files and use it for copy.
find /source/path -type f -mmin -60 -exec ls -al {} \; |\
while read file
do
cp -a "${file}" "/destination/path"
done

Find and rename a directory

I am trying to find and rename a directory on a linux system.
the folder name is something like : thefoldername-23423-431321
thefoldername is consistent but the numbers change every time.
I tried this:
find . -type d -name 'thefoldername*' -exec mv {} newfoldername \;
The command actually works and rename that directory. But I got an error on terminal saying that there is no such file or directory.
How can I fix it?
It's a harmless error which you can get rid of with the -depth option.
find . -depth -type d -name 'thefoldername*' -exec mv {} newfoldername \;
Find's normal behavior is to process directories and then recurse into them. Since you've renamed it find complains when it tries to recurse. The -depth option tells find to recurse first, then process the directory after.
It's missing the -execdir option! As stated in man pages of find:
-execdir command {};
Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.
find . -depth -type d -name 'thefoldername*' -execdir mv {} newfoldername \;
With the previous answer my folders contents are disappeared.
This is my solution. It works well:
for i in find -type d -name 'oldFolderName';
do
dirname=$(dirname "$i")
mv $dirname/oldFolderName $dirname/newFolderName
done
.../ABC -> .../BCD
find . -depth -type d -name 'ABC' -execdir mv {} $(dirname $i)/BCD \;
Replace 1100 with old_value and 2200 with new_value that you want to replace.
example
for i in $(find . -type d -iname '1100');do echo "mv "$i" "$i"__" >> test.txt; sed 's/1100__/2200/g' test.txt > test_1.txt; bash test_1.txt ; rm test*.txt ; done
Proof
[user#server test]$ ls -la check/
drwxr-xr-x. 1 user user 0 Jun 7 12:16 1100
[user#server test]$ for i in $(find . -type d -iname '1100');do echo "mv "$i" "$i"__" >> test.txt; sed 's/1100__/2200/g' test.txt > test_1.txt; bash test_1.txt ; rm test*.txt ; done
[user#server test]$ ls -la check/
drwxr-xr-x. 1 user user 0 Jun 7 12:16 2200
here __ in sed is used only to change the name it have no other significance

Find files older than X and Count them

Using Linux. What I need to do is determine the number of files in a directory(recursively) that are older than DATE and echo that number.
I have:
find /u1/database/prod/arch -type f -mtime +10 -exec ls -laR | wc -l \;
That lists the files fine.
And then I have:
ls -laR | wc -l
Which lets me count the files recursively.
But I can't seem to put them together. I think I need a script to do this but don't know how to do that.
Would love some help
find /u1/database/prod/arch -type f -mtime +10 | wc -l
works here.
You dont need the exec. use -print (or nothing) and find will print a line per file (and handle the recursion)
find /u1/database/prod/arch -type f -mtime +10 -print | wc -l

Resources