Tag Info

New answers tagged

1

You could do something like: efind() ( found=false for arg do if [ "$arg" = -regex ] && ! "$found"; then set -- "$@" -regextype posix-egrep found=true fi set -- "$@" "$arg" shift done exec find "$@" ) To insert -regex-type posix-egrep before the first occurrence of -regex.


5

since the find arguments are positional a function would be a better solution. find(){ command find "$1" -regextype posix-egrep "${@:2}" } since you want to "overwrite" the original command you need to use the full path of find so that your new function doesn't create an infinite loop of calling itself. by using a function instead of an alias we can ...


1

I think xargs is the more simple and direct solution: find . -iname *.gif | grep loader | xargs cp -t /home/me


2

GNUly: find . -iname '*.gif' -print0 | grep -z loader | xargs -r0 cp -t /home/me POSIXly: find . -name '*.[gG][iI][fF]' -path '*loader*' -exec sh -c ' exec cp "$@" /home/me' sh {} + (I used -path so it be equivalent with the grep solution, but it sounds like you want loader to be found in the name of the file as opposed to its directory components, ...


1

With zsh you could use the function age to print only the names of files that have been modified on a certain date: autoload age print -rl -- *.php(.e:age 2011/02/08:) or, if you want to search recursively: autoload age setopt extendedglob print -rl -- **/*.php(.e:age 2011/02/08:)


3

You can't use capture groups from the regexp in the command to execute. If you use find -regex to restrict matches, you'll have to do some extra matching in the command. You can do that by invoking a shell and using its own pattern matching constructs. For example, if foo and bar are constant strings and regex1 can't match bar: find … -exec sh -c ' ...


6

Assuming you are using GNU find or another find which has these options, find . -mindepth 8 -maxdepth 8 -name '*XYZ*' If you don't have such a find command, you can use this POSIX version: # must contain exactly 7 slashes find . -path '*/*/*/*/*/*/*/*' -prune -name '*XYZ*' -path '*/*/*/*/*/*/*/*' selects paths that are at least 8 levels deep (contain ...


0

If this is something you need to do somewhat frequently, you will probably find it useful to have a script to do it. Following is a quickly assembled Bash script which can do the job: #!/bin/sh dir_str=$1 src_dir=$2 dest_dir=$3 ret_code=0 if [ ! -e $src_dir ] ; then echo "Could not find source directory $src_dir." let "ret_code += 1" fi if [ ! -e ...


1

mv path_to_example_dir/*_jony /jony


0

With zsh: print -rl -- **/*.(rar|zip)(DoL) Replace with ls -lUd -- if you want to see ls -l information about them (-U being a GNU extension). Note that it sorts by size, not disk usage.


1

The possible duplicate Link answered the question partly. To provide multiple name patterns to 'find' use this find $directory -type f \( -name "*.zip" -o -name "*.rar" \) The complete answer to the question is: find $directory -type f \( -name "*.zip" -o -name "*.rar" \) -print0 | xargs -0 -n1 du -b | sort -n -r with $directory being comp_tuts/ dir


0

At the moment on RHEL7.1--I'm not sure if this has always been there and I missed it--I can execute the following to get a list of all the filecontexts and just pipe the output to grep: # semanage fcontext -l |grep ifconfig_exec_t /bin/ip regular file system_u:object_r:ifconfig_exec_t:s0 /sbin/ethtool ...


0

You could use sed: sed -i.BACKUP 's/\"Ticket_.*\"/\"Ticket_Final\"/g' <xml_file> Explanation: -i.BACKUP : It will substitute on the same file, but will keep a backup called xml_file.BACKUP, just in case it doesn't work as desired. s/ORIGINAL_REGEXP/SUBSTITUTION/g : Substitutes (s) the ORIGINAL_REGEXP for SUBSTITUTION, for every occurence (g). ...


2

Answering the second part of your question, the "best" way to watch for filesystem changes is by using inotify(7). There is a set of utilities that can hook into the kernel interface, inotify-tools. In particular, you want inotifywait from that set of utilities: inotifywait efficiently waits for changes to files using Linux's inotify(7) interface. It ...


3

The better way to change the column separator and use columncommand: sed 's/ */;/g' file | column -ts';' which produce: Item Code Active Description Store Room Row Shelf Bin On Hand 38NUTZSL Y 3/8"-16 HEX ZINC NUT B 03 C 2 0 WELD-AL Y WELD, ALUM. ROD, ...


1

In the unlikely event that your filesystem is mounted with strictatime or lazytime you could use: find /tools -atime +365 However if your filesystem is mounted with relatime, then this information is not being recorded. This is usually done for performance reasons. You can check using the mount command: $ mount /dev/sda3 on / type ext3 ...


0

Pure bash: shopt -s globstar for i in **/*.c;do read -N 1 h < "$i"; if [[ $h != "<" ]]; then # echo "found $i"; # do stuff with "$i" fi; done read -N 1 reads a single character from the file, without having to fork/exec anything. If you just need a list of filenames, use something else that makes it easier to use the ...


3

If your awk supports the nextfile statement (most do): find . -name '*.xml' -type f \( -size 0 -print -o -exec awk ' !/^</ {print FILENAME}; {nextfile}' {} + \)


4

You have some solid answers already, however I'll offer an alternative - the XML spec is quite strict, and files that don't start with < isn't actually XML at all. So a simple approach might be to simply test if the file is 'valid' or not. All XML parsers can do this, but here's an example: #!/usr/bin/perl use strict; use warnings; use XML::Twig; ...


1

find .//. -name \*.xml -type f -exec head -n1 {} + | sed -ne:n -e'\|^==> \.//\.|!{H;$!d' -e\} \ -ex -e'\|\.xml <==\n|!{G;x;d' -e\} \ -e's|[^/]*//\(.*\) <==\n[^<]*$|\1|p' head lists filenames all on its own. So you can just -exec it and have sed watch its input for head's report on the names for those files which don't match a ...


3

To grep the first line of each file and print if they match you can use xargs and awk find . -type f -name "*.xml" -print0 | xargs -0 -I{} awk 'NR==1&&!/^</' {} To print the filename of the files find . -type f -name "*.xml" -print0 | xargs -0 -I{} awk 'NR==1&&!/^</{print FILENAME}' {}


0

The following little loop will list a count of all files (excepting symlinks) in child directories of . which exist on the same filesystem as the child directory. for d in ./* ./.[!.]* ./..?* do ! [ -h "$d" ] && cd "$d" 2>&3 || continue printf "%s:\t" "$d" find .//. -xdev -depth ! -type l | grep -c '^\.//\.' ...


4

You have an NTFS filesystem. In this case you cannot safely fix the problem on anything except a Windows machine. (The Linux code is good, but I cannot recommend you trust it to fix a foreign filesystem.) Take the disk to your Windows system and run CHKDSK /F Q:, or whatever drive letter it's been assigned. Then try deleting the file. If that fails you're ...


0

Refer to Mat's comment. It is time for fsck. The situation you describe is rare. It appears you have at least two inodes pointed to dorothy[1].js + the directory entry for one inode is corrupt + thinks it's pointing to a directory. This should never happen, unless you're using dev/beta code for a filesystem. First run fsck. Then ensure you're running ...


0

try find * -print | awk -F/ '{c[$1]++;} END { for (c2 in c) printf "-%s -- %d\n",c2,c[c2] ;} ' where find from directory above the ones you want to sum up awk will count top level dir and file and sum up at the end.


1

Pure ksh93 solution: FIGNORE='@(.|..)' for dir in */; do a=( "$dir"/**/* ); printf "%s\t%s\n" "$dir:" "${#a[*]}"; done Result from /usr/src: linux-3.17.7-gentoo/: 561 linux-3.5.7-gentoo/: 517 linux-3.7.10-gentoo/: 505 linux-3.7.9-gentoo/: 513 linux-3.8.13-gentoo/: 551 linux-4.0.5-gentoo/: 1849


1

Will something like this suit your need: The path /boot is used for sample demonstration. Change it to the directory you need. for DIR in $(find /boot/* -maxdepth 1 -type d) do printf "%40s: %10d\n" "${DIR}" $(find ${DIR}|wc -l) done Output: /boot/grub: 282 /boot/grub/fonts: 2 ...


1

You could find the toplevel directories first, then use a second find, to count the number of files and directories within the toplevel directory: $ for dir in $(find . -maxdepth 1 ! -path . -type d | sort); \ do echo -n "$dir " && find $dir ! -path . | wc -l ; done ./adir 1151 ./anotherdir 140 ./623de41e44 280 ./examples 154 ...


0

Another Linux specific solution would be to use inotify(7) facilities ; you'll detect when files are added, and then you'll immediately run something to remove the older ones. OTOH, I guess that you might have some XY problem. Why do you have so many new files? Perhaps using sqlite, or GDBM indexed files, or some real database (e.g. PostGresQL, MariaDB, ...


2

If you want to skip the unreadable files: find /external/hd/folder ! -readable -print >unreadable_files rsync -a --exclude-from=unreadable_files /external/hd/folder/ /local/folder This assumes that you're using GNU find, and that your filenames don't have embedded newlines. If you want to copy all files, regardless of whether they are readable or not ...


1

If you mount the external HDD to your system locally, then it's not remote. Unless I misunderstood and your HDD is running a rsync daemon and say, connecting over a network? But forget that, can you just mount the drive locally and as @mjturner says, all you need to do is make sure you run rsync as root and it's a direct copy sudo rsync -a ...


0

Why not just use rsync to copy all of the files on the external HDD to the system HDD? It will not re-copy files that already exist. Of course, be sure to do the copy as root to ensure that you copy all files and preserve permissions.


0

I ended up just writing to a file: find ~/dim_import/* -type f ! -name xdir | cut -d '/' -f 5-6 > files and then looping through the "files" file and creating the directories from that and then copying the files there. while read line; do fileDir=`echo "$line" | cut -d '/' -f 1` # get folder name fileName=`echo "$line" | cut -d '/' -f 2` # ...


0

If you want to exclude that directory and all its descendents, then -prune is what you need: find "$HOME" -path "$HOME/.local/share/Steam/steamapps/common" -prune -o -empty -delete This means: if the path matches $HOME/.local/share/Steam/steamapps/common then -prune (ignore it and its children) and continue, OR if -empty then -delete (and continue).


0

Try: rm `find /home/tjuh -empty | grep -v "^/home/tjuh/.local/share/Steam/steamapps/common/"`


2

The -path argument is a pattern, so you have to use wildcards, or it will only match exact: find . -not -path "./.local/share/Steam/steamapps/common/*" -empty Also, you have to use absolute or relative paths, not mix them. The above one is with relative paths, this one is with absolute paths: find /home/tjuh -not -path ...


0

I've found that working with symlinks kill some of the other options. Especially the git specific answers. I've halfway created my own favorite out of this original answer that's pretty effecient. #!/usr/bin/env bash # usage: upsearch .git function upsearch () { origdir=${2-`pwd`} test / == "$PWD" && cd "$origdir" && return || \ ...


2

It means that the filesystem that is mounted on /hgfs (a filesystem of type vmhgfs, the VMware host/guest filesystem) doesn't support the stat system call. stat is used to return details about nodes on a filesystem and there's no requirement that every filesystem support it. You can ignore the error message. If you won't want it appearing in the output of ...


0

Since you already know how to filter the files, then use recursive copy: cp -R Or you could just use rsync with --prune-empty-dirs option. rsync --exclude='*xdir*' --prune-empty-dirs ~/dim_import ~/new Note: If you don't use trailing slash from source (like above's example) dim_import is also copied.


-2

find -type f -exec file {} \; | grep ELF | grep executable | cut -d: -f1


2

What are the %s %p options used here? From http://unixhelp.ed.ac.uk/CGI/man-cgi?find: %s File's size in bytes. %p File's name. Scroll down on that page beyond all the regular letters for printf and read the parts which come prefixed with a %. Are there any other options that can be used? There are. See the link to the manpage.


1

Here is a method that only uses POSIX shell features: find ~/dim_import/* -type f ! -name xdir -exec sh -c ' p=${1%/*}; d=${p##*/}; f=${1##*/}; mkdir -p new/"$d"; cp "$1" new/"$d"' -- {} \;


0

xargs should be available. You could use: find ~/dim_import/* -type f ! -name xdir | xargs -I {} cp {} new/{}


0

1) Add Alias globally for all users echo 'alias locate="grep $1 /var/locate/locate.db"' >> /etc/bashrc you could essentially point it to a shell script which you can do more intuitive argument selections... This should be adequate if you're just looking for very simple search. 2) Add Cron Job to run Daily. /etc/cron.daily #!bin/bash if [[ ! -d ...


0

If you know part of the name of a file you installed, e.g. studio, you can run locate studio The locate command finds files whose name contains the specified string. It uses an index which is updated every night (in a typical configuration; the index is updated by a cron job that calls updatedb with appropriate parameters), so you won't be able to locate ...


3

You could try sudo find / -name "studio.*". The way this command works is simple: sudo runs the find command as superuser (supersuser do) so you can check all the directories in the file system including the locked ones. find searches a given location (/ in our case) for a given file or directory. / is the parameter that dictates find where to search (in ...


0

apt-get purge should remove the package as well as all the config. And if you have removed the files with rm there's no way they could still be there. Are perhaps these "files" showing up in /proc? Because in this case you musn't worry, these aren't real files (/proc is just a pseudo filesystem intended to give you an interface on processes and other ...


0

Find honestly is the best way to go about this, but what I tend to do out of habit is grep -r something I know is in the file. It is definitely not the professional way to do things, but hey another option doesn't hurt. Quick tip, if you want to find commands by what they do, you can type man -k "describe what it does"


3

find <path>/. -type f -size 1033c ! -perm -0001 -ls


3

Get the file size: size="$(stat --printf="%s" "$path")" Get the path without the last extension: path_without_extension="${path%.*}" Compare the two: [ "${path_without_extension}.${size}" = "$path" ]



Top 50 recent answers are included