find -amin doesn't work if -name is excluded [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I am running GNU Linux 6.2 on x86_64 hardware. I'm in a directory with 1237 files. I want to list the files created in the last 36 hours and since I cannot get -atime to work, I use "-amin 2160":
$ find . -amin -2160 -name 'Ar*' -exec ls -l {} \;
-rw-r--r-- 1 oracle dba 2318 Aug 30 04:04 ./Archivelog_backup_08302015040300.log
-rw-r--r-- 1 oracle dba 2317 Aug 30 10:03 ./Archivelog_backup_08302015100321.log
-rw-r--r-- 1 oracle dba 1920 Aug 30 16:21 ./Archivelog_backup_08302015160300.log
-rw-r--r-- 1 oracle dba 2318 Aug 30 22:04 ./Archivelog_backup_08302015220300.log
-rw-r--r-- 1 oracle dba 2318 Aug 31 04:03 ./Archivelog_backup_08312015040300.log
-rw-r--r-- 1 oracle dba 2318 Aug 31 10:04 ./Archivelog_backup_08312015100320.log
But since I don't care what the name is and I want to see ALL files touched in the last 2160 minutes, I type this command,
find . -amin -2160 -exec ls -l {} \;
but it lists all 1237 files in the directory PLUS THE 6 that meet the criterion. Why?
Humbly,

Because one of the matches is the directory entry.
The real lesson here is to not use ls in scripts. find has excellent, unambiguous replacements like the -printf predicate. See also http://mywiki.wooledge.org/ParsingLs
You probably also want to add -type f to avoid listing directories.
find . -type f -amin -2160 -printf '%s %f\n'
What you put in the format string obviously depends on which information exactly you actually want to extract for each matched file.

The first name output by
find .
is
.
which, when sent to ls will list all the files in the current directory. In your first example, . was excluded by -name "Ar*" You could get the same effect by telling find to only emit regular-files (not directories) with
find . -type f …

Related

How to handle files with leading dashes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I am searching for files and directories using find, the files have leading dashes in the filenames:
ls -al
total 0
-rw-r--r-- 1 razhal staff 0 May 22 23:58 -x
drwxr-xr-x 3 razhal staff 96 May 22 23:58 .
drwxr-xr-x 12 razhal staff 384 May 22 17:06 ..
find * -maxdepth 1 -type file
The above gives the following error message:
find: illegal option -- m
I tried to terminate the options using --, but still having the same problem:
find * -maxdepth 1 -type file --
The strange thing is that if the folder contains a file without a leading dash I am getting no error message:
ls -al
total 0
-rw-r--r-- 1 razhal staff 0 May 22 23:58 -x
drwxr-xr-x 3 razhal staff 96 May 22 23:58 .
drwxr-xr-x 12 razhal staff 384 May 22 17:06 ..
-rw-r--r-- 1 razhal staff 0 May 23 00:03 x
find * -maxdepth 1 -type file
The above returns the x and no error message.
My question is how can I find and list both files/directories with and without leading dashes using find?
Notice that I really want to use find and not some other command such as xargs or similar.
Use . instead of *:
find . -maxdepth 1 -type file
. refers to the current folder you are in. You can also use .. instead of . to search from the parent directory.
Another option would be to put ./ in front of * like this:
find ./* -maxdepth 1 -type file
This way it won't interpret the files whose names start with a dash as options.

Susu Linux delete directory older than 1 day [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
Please help
I want to delete the directory and corresponding sub directories from a path and i tried the below command and for some reason it is not deleting .
Any help is appreciated .
/hana/export/PHO> ls -lrt
total 0
-rwxr-x--- 1 phoadm sapsys 678 Sep 4 10:59 export_schema.sh
drwxr-x--- 50 phoadm sapsys 4096 Sep 5 00:23 2018-09-05
drwxr-x--- 50 phoadm sapsys 4096 Sep 6 00:23 2018-09-06
-rwxr-x--- 1 phoadm sapsys 248 Sep 6 00:23 export_schema.out
phoadm#SS4100:/hana/export/PHO> find /hana/export/PHO/* -type d -ctime +1 -exec rm -rf {} \;
phoadm#SS4100:/hana/export/PHO> find /hana/export/PHO/* -type d -mtime +1 -exec rm -rf {} \;
phoadm#SS4100:/hana/export/PHO>
It should delete 2018-09-05 directory but for some reason the above command not working .
Thanks
I think it should be
find /hana/export/PHO -type d -ctime +1 -exec rm -rf {} \;
(without "*"), otherwise you are looking inside the directories not in PHO as you want, because "*" will be expanded by the shell.
or this:
find /hana/export/PHO/ -type d -mtime +1 -delete

How can I count the number of specific files within a date range on the command line using bash?

The request is just a basic count of files in specific directories. My current process using ipswitch:
Copy files from the month of March to my local directory (Windows)
Sort by name and, if necessary, delete files I do not need to count (from my local)
Select all or specific files, view the total, and enter it into a spreadsheet I maintain.
The spreadsheet is for monthly reporting on deliverable...
I know some unix/linux basics like directory navigation, somewhat about grep, and almost nothing about find.
I can't figure out how to search a directory for files within a specific date range, by month, and count how many files... sometimes with specific names or extensions. I thought
ls -l |grep ...something something...
might work, but I've hit a wall.
Let me know if more info is needed, sorry so wordy.
You can also use the -newerXY option to find to search for files within (and below) a directory between two dates directly. The date format can include a time with fidelity down to the second (e.g. "2017-04-13 21:35:58" or generically "yyyy-mm-dd h:m:s")
The generic form of the option is -newerXY where XY can be one of a access time, B birth time, c inode status change time m modification time and t interpreted directly as time.
To search between two given dates (say for November 2016), you can use a form of find similar to:
find /path/to/files -type f -newermt "2016-10-30 23:59:59" \
! -newermt "2016-12-01 00:00:00"
That will list all files between the end of October 2016 and the beginning of December 2016 (e.g. all files modified in November 2016), where
-type f limits the find to 'files' not 'files & directories'
-newermt "2016-10-30 23:59:59" files modified after the last second in October
! -newermt "2016-12-01 00:00:00" and not modified after the last second in November
Of course the '\' is simply a line continuation character used to split the line between the date tests so it doesn't generate a horizontal scroll-bar in this answer -- you can omit it and put everything on one line.
If you need to count the files, just pipe the output to wc -l as phatfingers shows in his earlier answer. An easy way to date-sort the files is simply to use find in command substitution as the argument to ls -t (along with any other options to ls you may desire), e.g.:
$ ls -lrt $(find . -newermt 2016-10-30 ! -newermt 2016-12-01)
-rw-r--r-- 1 david david 3180 Nov 9 13:02 ./inventory2.c
-rw-r--r-- 1 david david 769 Nov 19 23:00 ./xfree.c
-rw-r--r-- 1 david david 1375 Nov 20 16:14 ./reallocprob.c
-rw-r--r-- 1 david david 98 Nov 21 00:39 ./hw.c
-rw-r--r-- 1 david david 530 Nov 25 23:10 ./tools2/tools.c
-rw-r--r-- 1 david david 1175 Nov 25 23:15 ./tools2/main.c
-rw-r--r-- 1 david david 174 Nov 25 23:16 ./tools2/tools.h
-rw-r--r-- 1 david david 424 Nov 26 00:28 ./tools/tsave.c
-rw-r--r-- 1 david david 106 Nov 26 01:34 ./tools/tools.h
-rw-r--r-- 1 david david 909 Nov 26 01:53 ./tools/main.c
-rw-r--r-- 1 david david 525 Nov 26 02:45 ./tools/tools.c
-rw-r--r-- 1 david david 1732 Nov 27 23:53 ./nogets.c
-rw-r--r-- 1 david david 530 Nov 30 22:17 ./resolution.c
You can use find to filter and wc to count.
find . -mtime -$start -mtime +$end | wc -l
Because of the way -mtime truncates, and the exclusive nature of the comparison, it's a little tricky at first to get right, but doable.
find /some/path -mtime +0 # Find files modified prior to 1 day ago.
find /some/path -mtime -0 # Find files modified after 1 day ago.
find /some/path -mtime +1 -mtime -6 # Files modified between 2 and 7 days ago.
For example, say it's April 2, and you want all of March. Your $start would be 31, and your $end would be 0.
To run the same report on April 13, your $start would be 42 and your $end would be 13.

cant get rid of file with weird name/encoding on linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I have a file That appears as ? ? in ls:
drwxr--r-- 1 johndoe gid-johndoe 93756 Aug 22 09:10 .
drwxr--r-- 1 johndoe gid-johndoe 574633 Aug 22 09:18 ..
-rw-r--r-- 1 johndoe gid-johndoe 874857 Aug 12 15:25 ? ?
-rw------- 1 johndoe gid-johndoe 96342 Aug 22 08:41 .bash_history
When I try to grab the filename with sed, i get this strange output:
ls -la | sed 's/.*[0-9] //'
.
..
ash_history
I think the filename must have a weird encoding, but I dont know how to get a 'handle on it' so I can open/rename/delete it.
How can I get a handle to this file to open/rename/delete it?
Edit
When I type ls and hit tab to auto-complete, the file shows up as ^[ ^[
Thanks to this link from the comments I found a solution:
run ls -i to get inode number
run find . -inum <inode> -ok rm '{}' \; with the inode number to delete with 'are you sure?' prompt.
rm -i "$(ls|grep -E -m1 '\?|[[:cntrl:]]')"
be sure to use rm -i to verify matched files - match one at a time - the regex matches a literal questionmark as well as any ? displayed for invisible control characters
tested - created 3 files - with literal ? - with tab between ? - with ESC control characters - use CTRL-V to type control characters in a bash terminal
$touch '? ?' '? ?' '^[ ^['; ls -A1
? ?
???
? ?
$rm -i "$(ls|grep -E -m1 '[?[:cntrl:]]')"
rm: remove regular empty file '\033 \033'? y
$rm -i "$(ls|grep -E -m1 '[?[:cntrl:]]')"
rm: remove regular empty file '?\t?'? y
$rm -i "$(ls|grep -E -m1 '[?[:cntrl:]]')"
rm: remove regular empty file '? ?'? y
$ls -A
You should try with find and a regex.
find . -name "*" -exec ls '{}' \;
Find the test:
➜ t1 touch \?
➜ t1 ll
total 8.0K
-rw-rw-r-- 1 netsamir netsamir 0 Aug 22 19:02 ?
➜ t1 find . -name "*" -exec rm {} \;
rm: cannot remove '.': Is a directory
➜ t1 ll
total 0
➜ t1
Another solution is :
➜ t1 touch "\? \?"
➜ t1 ll
total 8.0K
-rw-rw-r-- 1 netsamir netsamir 0 Aug 22 19:05 \? \?
➜ t1 rm "\? \?"
➜ t1 ll
total 0
➜ t1

Renaming a '.' file in Ubuntu [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I downloaded a file using rsync and accidentaly provided it the destination as '.' (I thought it is the directory to download into). So it downloaded the multi-gig file but named it '.'.
drwxr-sr-x 2 root apache 4096 May 7 00:42 .
drwxr-sr-x 7 me apache 4096 May 7 00:25 ..
-rw-r--r-- 1 1006 1006 2008805206 Apr 5 04:49 .
-rw------- 1 root apache 1675 May 7 00:25 somefile
-rw------- 1 root apache 392 May 7 00:26 anotherfile.txt
How do I rename the 2GB+ '.' file to something meaningful. Nothing I do seems to work (i've tried mv, rename, etc.) but they all say
Device or resource busy
You can use this mv:
mv ./.[[:blank:]]* myfile
Or else try this find:
find . -type f -maxdepth 1 -name '. *' -exec mv '{}' myfile \;
Yes its a superuser question. But I found a solution elsewhere, so thanks everyone for trying. We could do this using:
find . -type f | (let i=0; while read f; do mv "$f" "$f"-$i ; let i=$i+1; done)
Not the most elegant way and probably very insecure too (as there is no undo).
Type this from the directory where it is located.
mv ./. newfile

Resources