rename multiple filename in linux [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 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

Related

Why I can not give the 'ln -s' command an operand through out the command sub $(ls ../*.txt)? [closed]

Closed. This question is not about programming or software development. 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 months ago.
Improve this question
ln -s $(ls ../*.txt)
When I do the command above it replay with an error message saying : "ln: target '../foo.txt' is not a directory".
foo.txt is the first file that ls command has found in the parent dir.
I am trying to make symbolic links for all the files in the parent directory which ends with ".txt".
Can you please explain why my command did not work ?
You forgot the directory name to put all the links into. If you want them put into the current directory, use ..
There's also no need to use ls, and you'll get the wrong results if any of the filenames contain whitespace or wildcard characters, because those will be processed in the $(...) output. Just use the wildcard directly.
ln -s ../*.txt .

Compressing file by file in gz in linux terminal [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 2 years ago.
Improve this question
I have several files in one folder and I would like to compress 1 by 1
Folder: /files
file1 to file1.gz
file2 to file2.gz
Etc
And that with all the files that are in the folder: /file
Is it possible to compress only the files (and not the folder) at once?
Looks like you are in search of looping mechanism to perform a GZIP. Below single-line execution can be a simple method to do it without any scripting.
for file_name in /files/* ; do gzip $file_name ;done
However, in case if the same directory holds directories as well as files, it better to have some beautified script for continuous use as below;
for file_name in /files/*
do
if [ -f $file_name ]; then
gzip $file_name
else
echo "$file_name is a directory."
fi
done
PS :-
else logic is purely optional and can be commented.
Try to keep this script outside of /files directory, else it would zip the shell script file as well.

How to Rename Files 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 5 years ago.
Improve this question
I want to rename all files in selected directory using rename command or move command from :
_02_mp3_cbr_320.m4a?anghakamitoken=sc245ae5a454547.5
_02_mp3_fsgsfsdfsfdfdsfcbr_320.m4a?anghakamitoken=sc245.ae5a
to
1.m4a
2.m4a
If those files always have a sheme like this:
_02_mp3_ * _320.m4a?anghakamitoken= *
You can do it like that:
#!/bin/bash
COUNT=0
for f in ./"_02_mp3_"*"_320.m4a?anghakamitoken="*; do
mv $f "$((++COUNT)).m4a"
done
This will result in
1.m4a
2.m4a
Assuming the initial files are in the same directory as the bash script.
Try this with GNU Parallel. it basically uses GNU Parallel's job number ({#}) as the number for renaming:
parallel --dry-run -k mv {} {#}.m4a ::: *m4a*
Sample Output
mv _02_mp3_cbr_320.m4a\?anghakamitoken\=sc245ae5a454547.5 1.m4a
mv _02_mp3_fsgsfsdfsfdfdsfcbr_320.m4a\?anghakamitoken\=sc245.ae5a 2.m4a
If the commands look correct, remove the --dry-run part and run it again. The -k keeps the output in order. The {} refers to the current file.
Make a backup before using any commands you are unfamiliar with...
To rename any file in Linux using mv (move) command:
mv (cfr. "man mv")
In this case, you need to enter the following lines on the command line:
$mv _02_mp3_cbr_320.m4a?anghakamitoken=sc245ae5a454547.5 1.m4a
$mv _02_mp3_fsgsfsdfsfdfdsfcbr_320.m4a?anghakamitoken=sc245.ae5a 1.m4a
It is important that you refer to the manual when you know the command you must use, to understand how to use it.

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 - update multiple symlinks with a single command [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
When cloning an environment that utilizes symlinks, the symlinks are copied (from production to clone) but they are still pointing to the original (production) files. I want them to be pointing to the cloned files, like:
Original:
/production/symlink1 > /production/directory/file1
/production/foo/symlink2 > /production/directory/sub/file2
After clone (now):
/clone/symlink1 > /production/directory/file1
/clone/foo/symlink2 > /production/directory/sub/file2
I want:
/clone/symlink1 > /clone/directory/file1
/clone/foo/symlink2 > /clone/directory/sub/file2
Is there a way to achieve it with a single command?
Have you created the links yourself? If yes, you could create them with -r parameter. See man ln:
-r, --relative
create symbolic links relative to link location
If the links were pointing to absolute paths, they will always point to the same paths after copying. That's the beauty of absolute paths and I don't think you can work around that.
You can try to rewrite those links after copying though. For example if you create a script relink.sh like this:
#!/bin/bash
for link; do
target=$(readlink "$link")
[[ $target =~ ^/production ]] || continue
newtarget=$(echo $target | sed -e s?/production?/clone?)
echo ln -snf "$newtarget" "$link"
done
Pass a list of symlinks to this script, it will check if they are pointing to some path under /production, and recreate the link with /production replaced with /clone. You can call it like this for example:
find /clone -type l -exec ./relink.sh {} \;

Resources