Tag Info

New answers tagged

1

You could do this: find -type f -printf '%TY-%Tm-%Td %.8TT %p\n' | sort -r | sed -r 's/(^[^ ]+ [^ ]+) (.+)/\2 \1/' | head -20 Putting .8 between % and TT modifies that field to limit it to 8 characters (hh:mm:ss). The sed regex moves the first part of the line (to sets of non-space characters with one space between) to the end of the line. It's ...


0

Assuming the rest of your command works correctly, you can just replace the last part xargs tar -xvpf with (cd /certain/directory; xargs tar -xvpf) and the tar command will be executed from this directory. This is a common useful 'trick' for executing commands relative to another directory. Note this only works in this because your tar file name (in ...


1

Use locate to find the file: locate <file_name> Then use mv to move the file mv <location/file_name> <new_location/file_name>


3

You can use find: find /usr -name '0914_Jul-2014.gz' -exec mv {} /var/tmp \; Or for extremely nested directory hierarchies find /usr -name '0914_Jul-2014.gz' -execdir mv {} /var/tmp \; Although as the documentation states you must ensure that your $PATH environment variable does not reference the current directory (namely .) if you use -execdir


0

Fractional 24-hour periods are truncated! That means that “find -mtime +1” says to match files modified two or more days ago. find . -mtime +0 # find files modified greater than 24 hours ago find . -mtime 0 # find files modified between now and 1 day ago # (i.e., in the past 24 hours only) find . -mtime -1 # find files modified less than 1 day ago (SAME AS ...


1

Instead of using -exec, which is simpler but less secure, you could just print the working directory. e.g. -execdir sh -c 'pwd ; rm -i "$1"' . {} \; Alternatively, print the whole path with -execdir sh -c 'readlink -e "$1" ; rm -i "$1"' . {} \;


2

To have the security benefit of -execdir (which is really about making sure the command doesn't get a path with several components) and get a prompt, you can use: find . -name hello.c -okdir rm -f {} \; (note that at least the GNU implementation of find doesn't escape filenames in a way suitable for terminals as GNU rm does (wrt control, newline, ...


5

The man page for GNU find describes -execdir in part thusly: 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. So there is no real subtlety involved. -exec invokes the command from the directory that you run find from (and thus needs to ...


6

you can use the --attributes-only switch of cp for this purpose, eg. find . -iname "*.txt" -exec cp --attributes-only -t dummy/ {} + From the man page of cp: --attributes-only don't copy the file data, just the attributes This will create empty files with all attributes of the original file preserved but no contents.


2

Without recreating the subdirectories: find . -type f -name '*.jpg' -printf /path/to/emptys/%f\\0 | xargs -0 touch


0

For the sake of comprehensibility and visibility I abuse an answer for explaining the quoting part. May the SE karma forgive me... This is (depending on the content of f) a special case, the problem is not easy to see: > f=foo > set -x > echo "reading entry: "$f"" + echo 'reading entry: foo' reading entry: foo The shells debug modus shows just ...


1

You could try running lsof with the PID but chances are the file was opened at run time by the application but then not kept open; lsof -p 15020 | grep app.js Alternatively you could strace the application and look through the output for the full path to the file; strace -p 15020


1

You put the -exec between the quotes that were around the end time, that is not what you want. You should do: find /mnt -type f -name "*.jpg" -newermt "2014-12-14 01:00:00" ! -newermt "2014-12-14 02:00:00" -exec cp -pf {} /home/pi/box/pictures/ \; (all on one line)


0

If your distro packages the ifconfig_selinux man page (Fedora has it in selinux-policy-devel), it'll tell you: The following file types are defined for ifconfig: ifconfig_exec_t - Set files with the ifconfig_exec_t type, if you want to transition an executable to the ifconfig_t domain. Paths: /bin/ip, /sbin/ip, /sbin/tc, ...


2

You could use find, and tell it to not descent into other filesystems (which should prevent it from accessing virtual filesystems like proc, sys, etc): find / -xdev -uid ${OLD_UID} -execdir chown ${NEW_UID} {} + This may not be as efficient. Another way to filter out the virtual files would be to remount the root filesystem somewhere else: mkdir ...


1

A portable way to prevent find from recursing is to execute the -prune action on directories other than the toplevel directory. find "$dir" \! -name "$(basename -- "$dir")" -type d -prune -o -mmin +3 -type f -print


-1

for i in *; do echo $i; ls $i | wc -l; done


0

You can use everything in one go. Also use + instead which will be faster as it executes the whole action at once. find /tmp -type f -iname 'magick-*'-exec stat {} + -exec sh -c 'rm -rf $@' _ {} + | tee -a log_to_file


1

You don't show what does happen when you try it. Just saying "it doesn't work" is NEVER correct. I suspect that your problems mey be related to one or more of the following: Quote the pattern or escape the *. If you don't, and you have one or more files in your current directory that matches that pattern, the pattern will be expanded to the list of ...


5

Option -0 of xargs means that output from pipe is interpreted as null terminated items. In such case you also need to create input for the pipe with find ... -print0.


6

Simple use: find . -size +1M -delete If you insist using xargs and rm with find, just add -print0 in your command: find . -size +1M -print0 | xargs -r0 rm -- Other way: find . -size +1M -execdir rm -- {} + From man find: -print0 True; print the full file name on the standard output, followed by a null character (instead of the newline ...


2

You can use getfacl tool with -Recursive option, -skipping files that only have the base ACL and pipe the output to grep. For example the following command run under /dev directory gives for me: $ getfacl -Rs . | awk -v RS= -v ORS='\n\n' '/\nuser:jimmij:rw-\n/' # file: sg1 # owner: root # group: cdrom user::rw- user:jimmij:rw- group::rw- mask::rw- ...


1

You might do... mkdir ../_cp pax -Xwrl -s'/"//gp' . "${PWD%/*}/_cp" That just creates a bunch of hardlinks to all files in the hierarchy rooted at . in ../_cp. You can then verify everything is well with both directories before removing one of them - they are pretty much the same directories after all, except in one directory there are no filenames which ...


3

xargs -0 -I {} mv {} {} | tr -d \" doesn't make sense: mv doesn't produce output. Thus you cannot build pipelines with mv. find . -name '*"*' -exec bash -c 'mv "$1" "${1//\"/}"' bash {} \; or with less overhead find . -name '*"*' -exec bash -c 'for file in "$@"; do mv "$file" "${file//\"/}"; done' bash {} +


4

Assuming you have the rename command installed, use: find . -name '*"*' -exec rename 's/"//g' {} + The rename command takes a Perl expression to produce the new name. s/"//g performs a global substitution of the name, replacing all the quotes with an empty string. To do it with mv you need to pipe to a shell command, so you can execute subcommands: find ...


1

Tried to write a Bash script. Please check if it can help you out. #!/bin/bash NOW=`date +%s` last_day() { case $MM in 01|03|05|07|08|10|12 ) echo "31" ;; 04|06|09|11 ) echo "30" ;; 02 ) echo "28" ;; esac } for file in `ls -1 bkp*` do # echo $file DATE=`echo $file | tr -cd [0-9]` # Extract Day of the Month from file name ...


1

I think you do not need to rename the files. You can transform the the filenames on the fly (first sed), compare them to a date (awk) and transform the matching filenames back (second sed). find parent/directory -maxdepth 1 -type d -name 'bkp_*' | \ sed 's#parent/directory/bkp_\(..\)\(..\)\(....\)#\3\2\1#' | \ awk -v date=$(date ...


0

The problem here is that the stdin (the standard input) for the command ran from xargs (in this case rm) is redirected from /dev/null, and the stdin is the file descriptor used by rm to obtain the user's confirmation. You could use the -a option so that rm obtain the list of files from an intermediate file previously generated by the find command (the -a ...


2

xargs reads data from stdin. When you use rm -i rm also tries to read the answer from stdin (try touch test && echo y | r -i test ; ls test) but stdin is closed by xargs (I assume) so rm reacts as if you had pressed ctrl-d at the prompt. Another solution might be find's -exec option: touch test find . -name test -exec rm -i {} \;


1

Why not just... for log in /var/log/*.[1-5] do whatever to "$log" done You don't need find as far as I can tell - the shell uses the same globs it does in -name. And if all of the files are in a single directory... Of course, if there are subdirectories you're also interested in then find could be beneficial - walking trees in the shell can be a headache. ...


3

By default, find includes everything in its search: directories, files, and symlinks. find "/path/to/dir" -mmin -30 -not -name ".*" -exec zip -r "testfile.zip" "{}" \+ If /path/to/dir was modified in the last 30 minutes, it will pass all the tests, and zip, since it was given the -r option, will add the directory and everything under it to the archive. ...


1

If you use the -prune option as suggested in this answer, the error message doesn't occur. Quoting from the above answer, Use -prune on the directories that you're going to delete anyway to tell find not to bother trying to find files in them. Testing mkdir koko cd koko touch file{1,2} cd .. find . -type d -name "koko" -prune -exec rm -rf {} \; ...


1

The problem is that find has found a directory, it matches your selection and then the command is executed. However, find wants to do what comes naturally, and that's recursing through a directory tree, but the directory it's just found has disappeared! Hence the error message. You can work around this by supplying the --depth option, which means process ...


3

In the first case, dump* is interpreted by the shell, and expanded into matching filenames, and then passed to find. In effect, find sees: find dumpa dumpb ... -type f ... In the second case, no interpretation is done by the shell. find does the filtering. Therefore, considering the recursive nature of find, the second method can locate files which the ...


2

Use -type f if you only want regular files. If on a GNU system, the -printf predicate can show you the date. find -maxdepth 1 -type f -mtime -50 -printf "%T+ %p\n"


0

Use the logical not operator ! or -not to exclude the path /var/tmp from the results. Note: -not is not POSIX compliant. find /var/tmp -type d -ctime -1 ! -path /var/tmp -exec rm -rf {} \; Or find /var/tmp -type d -ctime -1 -not -path /var/tmp -exec rm -rf {} \;


2

The first directory find /var/tmp finds is /var/tmp. If you want to skip that one (and use Gnu find) then you can change the command to: find /var/tmp -depth -mindepth 1 -type d -ctime -1 -exec rm -rf {} \; or find /var/tmp -mindepth 1 -type d -ctime -1 -exec rm -rf {} \; -prune Without -depth and -prune error messages may occur because rm -rf ...


1

The man page of find says this about -ctime: -ctime n File's status was last changed n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file status change times. and this about -atime: -atime n File was last accessed n*24 hours ago. When find figures out how many ...


0

This command remove the directories created in the last day. find /var/tmp -type d -ctime -1 -exec rm -rf {} \;


1

I'm going to focus on just the removal part of your question. If you have the list of filenames like this: $ cat data.txt 01012000 01022000 01032000 01012014 01022014 01032014 01042014 And you know that the cut off date for 6 months, is say "01022014". You can use sort & sed to determine which files need to be deleted, like so: This will reverse sort ...


2

You could convert the filename into something that can be compared directly (such as the Unix timestamp (number of seconds since the epoch), or to YYYYMMDD, which would be lexicographically sortable), and then check if it's older than six months. For example, a script like (say, at /path/to/compare.sh): #! /bin/bash LAST=$(date -d '6 months ago' +%s) for ...


0

You could use ranged brace expansion: Remove every half year: echo /path/to/directories/01{01..06}{2000..2014} OR remove everything except the last half year: echo /path/to/directories/01{01..12}{2000..2013} echo /path/to/directories/01{01..06}2014 Replace echo with rm -r and update the path.


1

The recursive grep will scan the entire tree and not care about directory structure. You need to traverse the structure and grep each directory individually. find /var/www -type d -print | while read dirname; do grep -sil '<?' "$dirname"/*.php | head -3; done The grep -s will handle conditions where there are no php files in a directory.


0

What about something like this? for DIR in $( find ./test -mindepth 1 -type d ); do find "$DIR" -type f | grep "\.php" | head -n3 done find ./test -mindepth 1 -type d lists all the directories in the test directory excluding the parent. find "$DIR" lists the full path in each directory and then greps for the php extension and lists three with head. ...


1

It is a slightly long, but it is a single command-line. This looks at the contents of the files and compares them using a cryptographic hash (md5sum). find . -type f -exec md5sum {} + | sort | sed 's/ */!/1' | awk -F\| 'BEGIN{first=1}{if($1==lastid){if(first){first=0;print lastid, lastfile}print$1, $2} else first=1; lastid=$1;lastfile=$2}' As I said, ...


1

You can use xargs to feed the output of a command as arguments to another: find . -iname '*.txt' -print0 | xargs -0 tar zcvf the_tarball.tar.gz Note here the -print0 from find and -0 from xargs work in conjunction to delimit file names correctly (so that names with spaces and such aren't a problem).


0

Create directory structure mkdir dir{A,B,C} touch dirA/file{,-001,2,3}.jpg touch dirB/file{A,A_ios,B,C}.jpg touch dirC/file{X,X_ios,X-001,Y,Z}.jpg Show multiplicity of duplicate files find . -name '*.jpg' -type f |sed 's/\(.*\/\(file.\).*\(.jpg\)\)/\2/' |sort |uniq -c|grep -v 1 Returns 2 fileA 3 fileX


0

You may want to consider programs specifically intended to the search of duplicate files rather than relying on the name, e.g. fdupes or fslint.


1

To complement the other answers, zsh, fish and (t)csh are more helpful here in that they can help you show your mistake before it becomes a problem: If there's no *test.txt file in the current directory: zsh$ find . -name *test.txt zsh: no matches found: *test.txt fish> find . -name *test.txt fish: No matches for wildcard '*test.txt'. find . -name ...



Top 50 recent answers are included