How to quickly rename a bunch of files in the folder - unix

I have a bunch of files that are named 'something_12345.doc' (any 5-digit number, not necessarily 12345). I need to rename them all to just 'something.doc'. This is a Unix filesystem, and I suspect there's a way to do this with just one command... Can any Unix regular expressions guru help?
Thanks!

#OP, the shell has already expanding your pattern for you, there in your mv statement, you don't have to specify the pattern for 5 digits again.
for file in *_[0-9][0-9][0-9][0-9][0-9].doc
do
echo mv "$file" "${file%_*}.doc"
done

This question was asked a lot of times on SO:
bash script to rename all files in a directory?
bash Linux - Massive folder rename
How to do a mass rename?
https://stackoverflow.com/questions/7137/replacing-one-string-in-a-bunch-of-file-names-with-another
My personal preference goes to mmv. But see "Mass Rename/copy/link Tools".

rename 's/_[0-9][0-9][0-9][0-9][0-9]//' *.doc

use sed
ls *.doc | sed 's:\([^0-9_]\)[0-9_][0-9_]*\.doc:$(mv & \1.doc)' | /bin/bash

Yes, rename takes perl style regular expressions. Do a man rename.

On FreeBSD, you might be interested in the sysutils/renameutils port. The command qmv opens your $EDITOR and allows you to specify all file renames in a reasonably comfortable environment. I personally prefer the qmv -fdo (single column) format.

Related

How to rename multiple filenames in cshell script?

I have a c shell script which has the following two lines, it creates a directory and copies some files into it. My question is the following - the files being copied look like this abc.hello, abc.name, abc.date, etc... How can i strip the abc and just copy them over as .hello, .name, .date.. and so forth. I'm new to this.. any help will be appreciated!
mkdir -p $home_dir$param
cp /usr/share/skel/* $home_dir$param
You're looking for something like basename:
In Bash, for example, you could get the base name, file suffix like this:
filepath=/my/folder/readme.txt
filename=$(basename "$filepath") # $filename == "readme.txt"
extension="${filename##*.}" # $extension == "txt"
rootname="${filename%.*}" # $rootname == "readme"
ADDENDUM:
The key takeaway is "basename". Refer to the "man basename" page I linked to above. Here's another example that should make things clearer:
basename readme.txt .txt # prints "readme"
"basename" is a standard *nix command. It works in any shell; it's available on most any platform.
Going forward, I would strongly discourage you from writing scripts in csh, if you can avoid it:
bash vs csh vs others - which is better for application maintenance?
Csh Programming Considered Harmful

Trim a file name in Unix

I have a file with name
ROCKET_25_08:00.csv
I want to trim the name of the file to
ROCKET_25_.csv
I tried mv but mv is not what I required because there will be cases where the files may be more than one.
I want the name till the second _.
How to get that in unix.
Please advise.
There are some utilities that provide more flexible renaming. But one solution that won't use anything other but included UNIX tools (like sed) would be:
ls -d * | sed -re 's/^([^_]*_[^_]*_)(.*)(\....)$/mv -v \1\2\3 \1\3/' | bash
This will only work in one directory, it won't process subdirectories.
It's not at all clear what you are actually trying to do, but if you just want to remove text between the last underscore and the period, you can do:
f=ROCKET_25_08:00.csv
echo ${f%_*}_.csv

Renaming files in directory with various endings on Mac?

Trying to rename a set of files in a directory with various filetypes, all with one common word, say 'foo', to another word, say 'bar' on a MacBook Pro.
E.g.:
foo.txt
form_foo.plist
home_foo.png
images_foo.zip
->
bar.txt
form_bar.plist
home_bar.png
images_bar.zip
Any ideas?
Use with care:
ls | grep foo | while read -r name; do echo mv "$name" "${name//foo/bar}"; done
That will report the commands it will run when you omit "echo". Inspect
the results, then rerun with "echo" omitted. This makes no attempt to work
on files with newlines in the name, nor does it recurse into subdirectories. If you want to work with files whose name begins with ., add -a to the invocation of ls. For safety's sake, you may want to add -i to the invocation of mv. Certainly make a backup first.
I don't have access to a Mac, but under Ubuntu you can use the rename command for this. Here's the man page in case that command is available

How to find files in a directory using grep with wildcards?

I have several hundred files with file names such as:
20110404_091415-R1-sometext
Another file name might be named:
20110404_091415-R1.2-sometext
What I would like to do is use the Unix grep tool in the terminal to find files that start with 2011 and also contain -R1 within the file name. Unfortunately, I have no idea to find files that satisfy both these criteria. I have tried to figure out a regex that would match this, but I am only a beginner programmer. Can anyone help please? Thanks in advance for your time.
why even use grep? I think ls 2011*R1* should suffice..
ls | grep "^2011.*-R1.*"
Should do the job
Just to find files, you can use ls 2011*R1* or echo 2011*R1*. To do something to files, use a loop (generally)
for file in 2011*R1*
do
....
done

paste without temporary files in Unix

I'm trying to use the Unix command paste, which is like a column-appending form of cat, and came across a puzzle I've never known how to solve in Unix.
How can you use the outputs of two different programs as the input for another program (without using temporary files)?
Ideally, I'd do this (without using temporary files):
./progA > tmpA;
./progB > tmpB; paste tmpA tmpB
This seems to come up relatively frequently for me, but I can't figure out how to use the output from two different programs (progA and progB) as input to another without using temporary files (tmpA and tmpB).
For commands like paste, simply using paste $(./progA) $(./progB) (in bash notation) won't do the trick, because it can read from files or stdin.
The reason I'm wary of the temporary files is that I don't want to have jobs running in parallel to cause problems by using the same file; ensuring a unique file name is sometimes difficult.
I'm currently using bash, but would be curious to see solutions for any Unix shell.
And most importantly, am I even approaching the problem in the correct way?
Cheers!
You do not need temp files under bash, try this:
paste <(./progA) <(./progB)
See "Process Substitution" in the Bash manual.
Use named pipes (FIFOs) like this:
mkfifo fA
mkfifo fB
progA > fA &
progB > fB &
paste fA fB
rm fA fB
The process substitution for Bash does a similar thing transparently, so use this only if you have a different shell.
Holy moly, I recent found out that in some instances, you can get your process substitution to work if you set the following inside of a bash script (should you need to):
set +o posix
http://www.linuxjournal.com/content/shell-process-redirection
From link:
"Process substitution is not a POSIX compliant feature and so it may have to be enabled via: set +o posix"
I was stuck for many hours, until I had done this. Here's hoping that this additional tidbit will help.
Works in all shells.
{
progA
progB
} | paste

Resources