Linux Wildcard Usage in Cp and Mv

linux wildcard usage in cp and mv

Let's talk about how wildcards work for a minute.

cp *.txt foo

doesn't actually invoke cp with an argument *.txt, if any files matching that glob exist. Instead, it runs something like this:

cp a.txt b.txt c.txt foo

Similarly, something like

mv *.txt *.old

...can't possibly know what to do, because when it's invoked, what it sees is:

mv a.txt b.txt c.txt *.old

or, worse, if you already have a file named z.old, it'll see:

mv a.txt b.txt c.txt z.old

Thus, you need to use different tools. Consider:

# REPLACES: mv /data/*/Sample_*/logs/*_Data_time.err /data/*/Sample_*/logs/*_Data_time_orig.err
for f in /data/*/Sample_*/logs/*_Data_time.err; do
mv "$f" "${f%_Data_time.err}_Data_time_orig.err"
done

# REPLACES: cp /data/*/Sample_*/scripts/*.sh /data/*/Sample_*/scripts/*_orig.sh
for f in /data/*/Sample_*/scripts/*.sh; do
cp "$f" "${f%.sh}_orig.sh"
done

# REPLACES: sh /data/*/Sample_*/scripts/*_orig.sh
for f in /data/*/Sample_*/scripts/*_orig.sh; do
if [[ -e "$f" ]]; then
# honor the script's shebang and let it choose an interpreter to use
"$f"
else
# script is not executable, assume POSIX sh (not bash, ksh, etc)
sh "$f"
fi
done

This uses a parameter expansion to strip off the tail end of the old name before adding the new name.

Copying files with wildcard (*) to a folder in a bash script - why isn't it working?

You got that exactly backwards -- everything except the * character should be double-quoted:

#!/bin/sh
dir_name=files

root=..
food_dir=food
fruits_dir=fruits

rm -rf "$dir_name"
mkdir "$dir_name"
chmod 755 "$dir_name"

cp "$root/$food_dir/"* "$dir_name/"

Also, as a matter of best-practice / convention, non-environment variable names should be lower case to avoid name conflicts with environment variables and builtins.

Copy multiple files with wildcard in bash

Brace expansions are an instruction for the shell about how to rewrite your command before glob expansion takes place. They aren't passed to the command itself -- cp has no idea if a brace expansion was used. For that matter, cp doesn't even have any idea if a wildcard is used; when you run cp *.txt dir/, the shell generates an array of C strings corresponding to something like cp foo.txt bar.txt baz.txt dir/ before running it.

This means that if you want to rewrite content after wildcard expansion takes place, you need to do it by hand.

for f in debug*.log; do
[[ $f = *_BACKUP.log ]] && continue # skip things that are already backup files
cp "$f" "${f%.log}_BACKUP.log"
done

How can I use wildcards to `cp` a group of files with the AWS CLI

To download multiple files from an aws bucket to your current directory, you can use recursive, exclude, and include flags.
The order of the parameters matters.

Example command:

aws s3 cp s3://data/ . --recursive --exclude "*" --include "2016-08*"

For more info on how to use these filters: http://docs.aws.amazon.com/cli/latest/reference/s3/#use-of-exclude-and-include-filters

How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

In Bash you can do it by enabling the extglob option, like this (replace ls with cp and add the target directory, of course)

~/foobar> shopt extglob
extglob off
~/foobar> ls
abar afoo bbar bfoo
~/foobar> ls !(b*)
-bash: !: event not found
~/foobar> shopt -s extglob # Enables extglob
~/foobar> ls !(b*)
abar afoo
~/foobar> ls !(a*)
bbar bfoo
~/foobar> ls !(*foo)
abar bbar

You can later disable extglob with

shopt -u extglob

Linux moving or copying multiple files with a shell

What about cp *.txt /dest/dir/?

And for adding .backup you could also do a loop that could look like this:

for i in *.txt
do
cp "$i" "/dest/dir/$i.backup"
done

how to change filenames using wildcards in bash

Do it as a batch process. Create a map file with columns fromName toName, run through a script for renaming.

while read a b; do mv "$a" "$b"; done < filemap

to create filemap, ls and paste

ls -1 > files; paste files files > filemap; rm files

and manually edit the filemap file to add required prefixes.



Related Topics



Leave a reply



Submit