Change of directories names according to pattern in Bash - bash

I need to change with one-liner (best option) directories name, more precise - to remove some prefix that has variable part at the beginning TO_BE_REMOVED_W._
I can find dirs to modification with:
find ./ -type d -iname 'TO_BE_REMOVED_W*' -exec mv {} _____ \;
How should I form execution part to rename directories to remove pattern from whole name?

Most distros come with rename which accepts substitution patterns, so I usually use -execdir with rename.
For example, to remove PREFIX_:
$ find . -execdir rename 's/^PREFIX_//' '{}' +
Note that for Arch Linux, this is called perl-rename (Arch's default rename is not the standard rename found on most distros).

You can use -exec bash -c '...' like this:
$ find -name 'foo*'
./foo-01
./foo-05
./foo-02
./foo-03
./foo-04
$ find -name 'foo*' -exec bash -c 'file=$0; echo Now you can do anything with $file ...' {} \;
Now you can do anything with ./foo-01 ...
Now you can do anything with ./foo-05 ...
Now you can do anything with ./foo-02 ...
Now you can do anything with ./foo-03 ...
Now you can do anything with ./foo-04 ...
$

Related

Find and rename files by pattern works in Debian, but not in CentOS7

I need to find and rename files with question mark in names.
Example: "style.css?ver=111" should become "style.css"
I use this command
find . -type f -name "*\?*" -exec rename 's/\?.*//' '{}' \;
In Debian all works fine, but in CentOS7 I get and error that "rename: not enough arguments
"
Any ideas why?
For a reliable option that should work in any POSIX-compliant system, you may use
find . -type f -name "*\?*" -exec sh -c 'mv -- "$1" "${1%%\?*}"' findshell {} \;
$1 is the name of each file found and ${1%%\?*} is a construct that strips the substring starting from the question mark.
That should be enough if you have a few matching files. If you need it, a more efficient alternative is
find . -type f -name "*\?*" -exec sh -c '
for file in "$#"; do
mv -- "$file" "${file%%\?*}"
done
' findshell {} +

Script that replaces all "*.html" file names with "*.html.<username>"

Write a script that replaces all *.html file names with *.html.<username> in the current directory.
You could use find's -exec option:
$ find . -name "*.html" -exec mv '{}' '{}'.${USER} \;
If you have perl-rename installed (called rename on Debian-based and many other systems, /usr/bin/site_perl/rename on others), you can run:
rename "s/\.html$/.html.$USER/" *
If you don't have the perl rename, but have the compiled one, you can do:
rename '.html' ".html.$USER" *
If you have neither, use the shell:
for f in *.html; do mv "$f" "$f"."$USER"; done

How to delete a file except in any one of the subdirectory using shell script

Hi I want to delete a file from any of the subdirectories except one of the subdirectory.
For ex
folder a->a.txt
folder b->subdir 1 -> msgdir-> a.txt
folder c->
Now i want to delete a.txt only in folder a but not the file in msgdir .msgdir can be in any level of subdirectories as it would be changing.
Please help me to resolve this.
This will ignore specifically the msgdir at any level and remove a.txt except in msgdir.
find . ! -path '*/msgdir/*' -name a.txt -type f -delete
Tested with GNU find 4.4.2 and BSD find (Mac Yosemite).
The following approach is overkill if you have GNU find (or a newer BSD one), with the -path option. Otherwise, read on...
You haven't specified which shell you're using but if you have bash, you could go with something like this:
find -name a.txt -exec bash -c "[[ '{}' != */msgdir/* ]]" \; -print
This filters out paths containing /msgdir/, as the test will only pass if the file path doesn't contain the string. If you're happy with the results, you can change -print to -delete.
Without bash, you could use grep to determine the match:
find -name a.txt -exec sh -c "printf '%s' '{}' | grep -qv '/msgdir/'" \; -print

BASH: find and rename files & directories

I would like to replace :2f with a - in all file/dir names and for some reason the one-liner below is not working, is there any simpler way to achieve this?
Directory name example:
AN :2f EXAMPLE
Command:
for i in $(find /tmp/ \( -iname ".*" -prune -o -iname "*:*" -print \)); do { mv $i $(echo $i | sed 's/\:2f/\-/pg'); }; done
You don't have to parse the output of find:
find . -depth -name '*:2f*' -execdir bash -c 'echo mv "$0" "${0//:2f/-}"' {} \;
We're using -execdir so that the command is executed from within the directory containing the found file. We're also using -depth so that the content of a directory is considered before the directory itself. All this to avoid problems if the :2f string appears in a directory name.
As is, this command is harmless and won't perform any renaming; it'll only show on the terminal what's going to be performed. Remove echo if you're happy with what you see.
This assumes you want to perform the renaming for all files and folders (recursively) in current directory.
-execdir might not be available for your version of find, though.
If your find doesn't support -execdir, you can get along without as so:
find . -depth -name '*:2f*' -exec bash -c 'dn=${0%/*} bn=${0##*/}; echo mv "$dn/$bn" "$dn/${bn//:2f/-}"' {} \;
Here, the trick is to separate the directory part from the filename part—that's what we store in dn (dirname) and bn (basename)—and then only change the :2f in the filename.
Since you have filenames containing space, for will split these up into separate arguments when iterating. Pipe to a while loop instead:
find /tmp/ \( -iname ".*" -prune -o -iname "*:*" -print \) | while read -r i; do
mv "$i" "$(echo "$i" | sed 's/\:2f/\-/pg')"
Also quote all the variables and command substitutions.
This will work as long as you don't have any filenames containing newline.

Rename files with dynamic name in bash

I am trying to rename all files which contains 'Name' with dynamic name.
So this file 'NameSomething' should look like this 'SearchSomething'. But script below just removes 'Name' from file name.
name='search'
Name='Search'
find ../../$name-module -name 'Name*' -type f -exec bash -c 'mv "$1" "${1/Name/$Name}"' -- {} \;
You have to export Name, because otherwise the command find will not inherit the variable $Name into its environment. So:
name='search'
export Name='Search'
find ../../$name-module -name 'Name*' -type f -exec bash -c 'mv "$1" "${1/Name/$Name}"' -- {} \;
\You can escape quotes, no need for an export:
find ../../$name-module -name 'Name*' -type f -exec bash -c "mv \"\$1\" \"\${1/Name/$Name}\"" -- {} \;
You might use find to find a list of the files you want to rename and pipe that to another program to do the renaming.
For example, you could do:
find ../../$name-module -print0 -name 'Name*' -type f | \
xargs -0 rename "s/Name/$Name/"
No need for exporting (ugly + might affect other commands)
Rename might get run only once (if all the found files fit on one command line) (with -exec, you're running two programs -- bash and mv -- per each file. That might be inefficient.
(You could get further performance increase with this approach by using Gnu parallel instead of xargs, if you're on a multicore computer)
You could use the Perl-based rename command:
Name=Search
rename "s/^Name/$Name/" *

Resources