New answers tagged

0

To print nicely quoted and escaped filenames (correctly handling apostrophes and quotes together) try the following: find . -exec printf "%q\n" {} \; which will produce (for example) . ./DSC07051_.jpg ./Photos "./Photos/TestDSC9910'Apos .jpg" './Photos/TestDSC9913 Space.jpg' ./Photos/DSC07053+.jpG ./Photos/DSC07048.jpg ./Photos/Photos....


5

If you write find ... -exec foo | bar \; the vertical bar is interpreted by your shell before find is invoked. The left hand of the resulting pipeline is find ... -exec foo, which obviously gives a "missing argument to `-exec'" error; the right hand of the pipeline is bar. Protecting the vertical bar from the shell, as in find ... -exec foo \| bar ...


1

OK, after fiddling with this and patching together different suggestions from elsewhere, I came up with this: sudo su -c "rsync -vrt --delete --from0 --files-from=<(cd /home/dir; find . -type f -mmin -2200 -print0 ) '/home/dir1' Maildir" This will cd into the source directory first before running the find command. I also use -print0 argument ...


1

The issue is that you want to use GrepOptions, an array, as an environment variable in your code. You can't do that since arrays can't be exported. Instead, you will have to pass the options in to you bash -c script along with the pathname that you want to run grep on. Below, I've taken it a bit further and also pass the regular expression, and more than a ...


-1

bash -c 'grep ${grepOptions+$grepOptions} ....' grepOptions is a shell variable housing the options to grep, space separated. naking use of ${var+alternative} ; in case options are nonnull then use them else an e empty unquoted is used which dissolves in parsing It is impled that shell variables are exportable.


4

With zsh, you could do: set -o extendedglob LC_ALL=C find / -regextype egrep \ -regex ${(j[|])${(u)path:P}//(#m)[][.\$^*()+{}\\|.]/\\$MATCH} -prune -o \ -type f -executable -exec file {} + $path is a special array tied to the $PATH env var. $path:P gets the realpath (absolute, canonical, without symlinks) of each member ${(u)array} removes duplicate (...


6

Assuming GNU find and the bash shell (as is used in the question), this is a short script that would accomplish what you're trying to do: #!/bin/bash IFS=: set -f args=( -false ) for dirpath in $PATH; do args+=( -o -path "$dirpath" ) done find / \( \( "${args[@]}" \) -o \ \( -type d \( ! -executable -o ! -readable \) \...


2

f expects its argument as the next command-line argument; with tar xvf --skip-old-files {} that’s --skip-old-files, which is thus interpreted as the file to be read, not as an option. Use tar xvf {} --skip-old-files instead.


0

There is no day number 0 in a month and there is no month 0 in a year. Days and months start from 1. Try one of these instead: touch -d '1/1/0 0:0' foo touch -t 000001010000 bar


0

Here is a simple way to do it that does not rely on special flags in find. It works for any input into xargs. Just use sed to wrap each line in quotes. find . -size +1M | sed 's/.*/"&"/' | xargs ls -l You can replace ls -l with rm after you are satisfied it works.


4

chmod can do this by itself: chmod -R g+o dir A combination of the letters ugoa controls which users' access to the file will be changed: [...] other users in the file's group (g), [...] The operator + causes the selected file mode bits to be added to the existing file mode bits of each file [...] The letters rwxXst select file mode bits for the affected ...


1

find . -type f -name "*.sas" -exec sed -i'_new' 's/2020/2021/g' {} \;


2

Using GNU find and a loop, you could search for each of the r w x permissions which is not set for the group, but set for others and set it for group: for perm in r w x; do find . -type f \! -perm /g+"$perm" -perm /o+"$perm" -exec chmod g+"$perm" {} \; done


1

With the GNU implementation of find, the one generally found on non-embedded OSes that use Linux as their kernel (though that syntax is not GNU specific as it was actually first introduced as a user-contributed patch for BSD), you can do: find / -type f -newermt 2020-01-01 ! -newermt 2021-01-01 Note that it will include files last modified at exactly 2021-...


1

You won't need rsync for this. For each file, you need to find it and then copy it to the destination src='/var/www/html/source-directory' dst='/var/www/html/target-directory' while IFS= read -r file do echo "Checking '$file'" >&2 find "$src" -type f -name "$file" -exec cp -a {} "$dst/" \; done <list....


0

To list all files except those in .git I use this command find -type f -not -path './.git/*'


5

find /data -type f \( -name "stdout" -o -name "stderr" \) -size +1000M -print is probably very close to the fastest you can do using common shell scripting tools. The size of the individual files is not relevant, because it is recorded separately from the contents. That is, find doesn't actually count the number of bytes in each file to ...


0

Use file $(locate -r 'xfce4-keyboard-overlay$') | grep -v directory$ | awk -F: '{ $(NF--)=""; print }' Explanation: locate ... finds all files and directories file $(...) appends a message at the end of each line with the type of file grep -v ... reverse-filters lines containing directory$ awk ... removes what file ... appended


0

For future readers, the POSIX compliant solutions that do not traverse the excluded directories and do not include them in the output at all have the general formats: Single exclusion find searchpath \! \( -path searchpath/excludepath -prune \) Multiple exclusions find searchpath \! \( \( -path searchpath/excludepath1 -o -path searchpath/excludepath2 -o -...


0

There should be no need to stop/start the services when using the abrt-cli tool. The full documentation on the tool is here: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/deployment_guide/sect-abrt-cli Further, you can call the abrt-cli tool to remove individual directories under /var/spool/abrt instead of the wildcard *. ... ...


0

base='/var/www' tmpf=$(mktemp) f1="$base/file1.txt" f2="$base/file2.php" ws='[[:blank:]]\{1,\}' re_bre="define[(]$ws'AUTH_KEY'" sed -e "/$re_bre/q;d" "$f1" | sed -e "1h;/$re_bre/g;1d" - "$f2" > "$tmpf" && mv -f -- "$tmpf" "$f2"


0

This could be an option: # save the AUTH_KEY into a variable $ key=$(awk -F"'" '/AUTH_KEY/ { print $4 }' file1.txt) # use the variable to replace the same value in the other file $ awk -F"'" -v val="'${key//\\/\\\\}'" 'BEGIN { OFS="" } $4 = val,$2 = "'\''"$2"'\''"' file2.php $ cat file2.php | grep ...


1

awk ' /define\([ \t]*\047AUTH_KEY\047,/{ if (FNR==NR){ line=$0 } else{ $0=line } } FNR!=NR ' /var/www/file1.txt /var/www/file2.php > /var/www/file2.php.new mv /var/www/file2.php.new /var/www/file2.php The bracket expression [ \t] matches a space or tab character, the * matches the pattern zero or more times. \047 is a octal single quote '. ...


3

Try this: find . -type d -regex ".*/zing/[^/]+"


0

You could find all zing directories find . -type d -name zing and execute a "ls" in each if you need to get the directory's contents. Or maybe iterate the find (find \{\} -type d -maxdepth 1 -mindepth 1) to get only the immediate child directory names. Then you can use basename to get only the directory name, or some ugly hack like | rev | cut -f1 ...


5

Bash, Globstar activated (with shopt -s globstar). printf '%s\n' **/zing/*/ For only the last path component, for dir in **/zing/*/; do basename "$dir"; done POSIX Find. find . -type d -path '*/zing/*' -prune -prune avoids descending in the matched directories. For only the last path component, find . -type d -path '*/zing/*' -prune -exec ...


1

find ... -exec sh -c 'printf "%s\0" "$@"' - {} + Simply find ... -exec printf '%s\0' {} + may work too, though that will obviously use the standalone printf executable instead of the shell's builtin. I'm not sure if that may have other implications.


0

First I'll set up some fake data: $ for fileno in {1..4}; do for line in {1..100000}; do printf "%d,%d,%d,%d,%d,%d,%d,%d\n" $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM; done > ~/tmp/big-fake-${fileno}.csv; done $ for fileno in {1..4}; do for line in {1..50000}; do printf "%d,%d,%d,%d,%d,%d,%d,%d\n" $RANDOM $RANDOM $...


0

Could you have duplicate filenames? If so they would overwrite existing and reduce the total number of files ending up in the destination. You could try this and see what this gives you, it should count only the numberof unique basenames of your found files: find /tmp/files/test/ -type f -size -1000c -print0 | xargs -n1 -0 basename | sort | uniq | wc -l


2

sed has a -c option to use with -i so that it does copy instead of move, which makes this work. This works: RUN sed -ci "s/localhost/foobar/g" /etc/hosts Note carefully, however, that sed -ic won't work, as it would make a backup file ending in .c, rather than doing what sed -i -c or sed -ci does. Cleaned up a docker script today from this ...


7

Your distribution uses UTF-8 character encoding. This is normal for most current distributions. What you see is the effect of UTF-8 coded characters displayed as another encoding. Many GNU utilities try to use different quotation marks for opening and closing quotes. With some fonts this looks good, with others not so good. Let's look at the output produced ...


1

this is a codes on start and on end. â<80><98> = ‘ â<80><99> = ’


2

Since you are using grep to search using a regular expression, you have to be aware that grep by default interprets the search string as basic regular expression (BRE). The syntax you use contains extended regular expression (ERE) syntax, so you would need to use the -E flag. Copying the string example you posted into a file test.php, the call ~$ grep -E '\$...


2

you can do several things, using "globbing" In a nutshell: the shell tries to match ? to any character, (unless it is "protected" by single or double quotes * to any string of characters (even empty ones), unless protected by single or double quotes [abc] can match either 'a', 'b' or 'c' [^def] is any single character different than 'd', ...


1

You need to specify both source and target arguments to rsync, even when you're reading pathnames from a file: echo /tmp/file | rsync -av --files-from=- / user@server:/ Here, the pathnames would be relative to the / source directory. The -R option is implied when using --files-from, and standard input may be specified with -. See also man rsync Why is ...


Top 50 recent answers are included