Unix Renaming Files [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
I want to rename files in a folder on UNIX using a script.
The format of the original file is:
abc.txt.temp
and I want to rename it to:
abc.txt
Many files use this format and I want to remove .temp from the original file name.

The answer Ciprian gave is certainly an option but I feel it's limiting.
The solution below is much more flexible as you don't have to actually count anything and you can remove text from any position rather than just the end.
The following command (1 line) will remove any mention of .temp in all the files:
for filename in *; do mv "$filename" "${filename//.temp/}"; done
Note The "*" means all files in current folder. You can use *.temp to achieve exactly the same result as Ciprian's method. (that is, only removing .temp from files ending with .temp)

I don't know about UNIX, but since the question also have the Linux tag it may just be a UNIX/Linux confusion.
Most GNU/Linux distributions have a rename command. Depending on the rename version, to replace foo with bar in files names the syntax may either be as simple as
rename foo bar files
or follow sed's regexp syntax :
rename 's/foo/bar/' files
In your case, you want to replace .temp with an empty string ('') in all files ending with .temp, so depending on your rename version one of these commands should work :
rename .temp '' *.temp
or
rename 's/\.temp$//' *.temp

Create the following script with a name like 'rename.sh':
#!/bin/bash
TARGET_DIR=$1
TARGET_FILES="$TARGET_DIR/*.temp"
for fileName in $TARGET_FILES
do
newFileName=${fileName::-5}
mv -v "${fileName}" "${newFileName}"
done
note The ${var:offset:length} expansion requires bash version 4 or higher.
Give it execution rights:
chmod a+x rename.sh
You need to call it and pass the name of the directory of the .temp files as a parameter. Call it like this:
./rename.sh /path/to/the/temp-files
The script loops over all the *.temp files in the target folder, extracts the last 5 chars from the file path ('.temp' is 5 chars) and moves the original file to the new file that doesn't contain .temp as the extension.
EDIT: tested on a CentOS 7

Related

How to undo "mv file -" command in Ubuntu [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
As far as you know, - has different usages for in combination with different commands in Linux. For example in combination with cd command, it means "Change directory to the previous location" and in combination mv or cat command it means stdin or stdout.
Unfortunately I wrongly used this character with mv command. I wanted to move my file to the previous location which I have been before the change directory but I moved it to stdin instead.
Is there any way to recover my file?
I run this command:
# mv myfile -
I moved it to stdin instead.
No, you moved to a file literally named by a dash (you'll use /dev/stdin or /proc/self/fd/0 to refer to the stdin, i.e. the file descriptor 0).
You want to
mv -i ./- myfile
this is usual practice (to prefix by ./ a strange path). The -i interactively asks for confirmation.
Sometimes it is not even easy to type a path for a weird file (e.g. for a file name containing a single newline character). You could use globbing (read about shell expansion), e.g. mv -i ./? file.
See mv(1) and path_resolution(7) and glob(7) and proc(5)
The good habit is to avoid strange characters in file paths.

Linux symlink all files in directory is adding '*' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I'm trying to symlink all files in a directory to a target directory, by doing:
ln -s /directory/* /target-directory
Problem is when I go into the target-directory, I'm seeing this '*', an asterisk in quotes, instead of all the files in the first directory. What am I doing wrong? Thanks.
Normally, what'd happen when you run ln -s /directory/* /target-directory is that the shell would expand /directory/* into a list of the (currently existing, visible) files in /directory/, and then pass that to ln in its argument list. The result would be equivalent to something like ln -s /directory/file1.txt /directory/file3.pdf /directory/file3.c /target-directory. Note that the ln command would not see the "*", and so would not include it in either the link source or target name.
Since "*" is being used as the link name, it's not getting expanded. There are a couple of reasons this might happen:
You might have the noglob shell option set. But you said in the comments that's not the case.
The shell expansion might not have matched any files, in which case the shell will simply pass it unchanged to ln, giving the result you describe. You said you created a file in the source directory, but did you re-test after doing that? Another possibility is that there's a typo in the directory path, so it's not finding a matching directory (let alone any files in it).
Oh, one more note: you said when you go to the target directory, you see an asterisk in quotes. Exactly how are you looking? Because if you're just using ls, it should not include quotes in the listing unless they're actually part of the filename. [Edit: Mark Plotnick pointed out that some versions of GNU ls do add quotes to some filenames.] I have no idea how the command you gave could add quotes to the filename.
Do not create the destination directory and do
ln -sd ./source ./destination
If you set the failglob option, you will get an error message if expansion of * is not possible.
shopt -s failglob

Why do I need to put dot forward slash when running a 'sh' command in Linux? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I have found that if I have a .sh file in a certain directory then I can run it (using sh) from that folder with or without dot forward slash. So why even bother adding it?
Please check http://www.linfo.org/dot_slash.html as well.
Basically this is a safety mechanism to indicate the program to execute is in the current directory and NOT a built-in command, a command in some other folder specified in the PATH etc.
From the link:
Files in the current directory can be accessed for reading and writing by merely entering the command name (e.g., cat or vi) followed by the name of the file. That is, no absolute path is necessary. However, when execution is desired, either an absolute path (or its dot slash equivalent) or the inclusion of the directory containing the command's executable file in the PATH variable is necessary. This is a built-in safety mechanism.
If your script is running without ./ then it means the specific location is in the path or . is in the path as mentioned in comments.
Hope it helps.
Your PATH is a list of directories which is searched to find files to execute. If your command is in one of those directories it will be executed. If it is in a different directory, then you need to indicate to the shell what directory it is in. . means the current directory, so
./prog
will run the prog file found in the current directory
/some/other/path/prog
will run the file in the directort /some/other/path
and
subdir/prog
will run prog in subdir relative to your current directory.
To see what your path is set to type echo $PATH.

rename multiple filename in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I have many files named xxxx.min.js and want to rename the files to xxxx.js so basically want to remove .min only.
Is there a command I can use to do this job?
I thought using rename command would be easy for each single file, but that would take forever since I have many of them.
any idea?
Here's a bash-only command (not requiring Perl)
for i in *.min.js; do a=$(basename $i .min.js); echo mv $i $a.js; done
Explanation
for i in *.min.js; do
loop over all files matching *.min.js
a=$(basename $i .min.js)
extract the base name of the file (i.e. strip off .min.js) and save the result in $a
echo mv $i $a.js
for now, print to the console the command that WOULD be run if you removed the echo
When you are satisfied that it generates the correct commands, remove the echo to actually rename the files.
Ubuntu and Debian linux distribution both have a perl version of mv function called rename or prename, which supports regexp. The manual can be found here.
Go to the folder of the files and run the command as follows:
rename s/\.min\.js$/\.js/ *.min.js

rename multiple files shell in Linux [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a number of files such as file_022.bmp, file_023.bmp...file_0680.bmp. I need to rename these to something a little bit more convenient such as file_1.bmp, file_2.bmp...file_658.bmp.
Is there a bash script that I could write to do this for me? Thanks for the help and advice.
Luke H
if you're on a debian based linux system then you can use the rename script which accepts regular expressions to rename files. Some more info because I find it hard to find the man page.
e.g.
harald#Midians_Gate:~$ ls p*.php
parse.php pd.php pgrep.php preg_based.php proc.php
suppose I want to change the extension to .perl and prepend the name with file_
then I use command:
rename -n 's/([a-z]*)\.php/file_$1.perl/' p*.php
would give
parse.php renamed as file_parse.perl
pd.php renamed as file_pd.perl
pgrep.php renamed as file_pgrep.perl
preg_based.php renamed as preg_file_based.perl
proc.php renamed as file_proc.perl
I select and capture the base filename ([a-z]*) and then use it in the substitution $1 and append .perl and prepend $1 with the regular string file_
the -n option makes it test run without changing anything
As you can see from this example your selecting regexp needs to be correctly thought out or you get cases like the above preg_based.php where you wanted file_preg_based.perl :)
to compensate for that I would've needed to use ([a-z_]*) here
It's one of the many reasons why I keep hanging on to debian, I'd love to find the equivalent for other non-debian systems though :-/
if you have files a.bmp,b.bmp,c.bmp
and you want to end up with file_1.bmp, file_2.bmp, file_3.bmp
using bash:
mkdir result
index=1
for i in *.bmp
do
mv "$i" "result/file_"$((index++)).bmp
done
notes:
using a subdirectory is advised to avoid accidentally overwriting a file that looks like file_xx.bmp
if you have too many files to fit in the command line after expansion you could use something like:
mkdir result
index=1
find . -name "*.bmp" | while read i
do
echo mv "$i" "result/file_"$((index++)).bmp
done
after inspecting the output remove the 'echo'

Resources