Linux - update multiple symlinks with a single command [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
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 {} \;

Related

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 quickly move to a real directory using a soft link directory 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 a dir /var/real-dir
I've created a soft link to it like this
ln -s /var/realdir /var/virtual-dir
Having that my working directory is /var/virtual-dir, I'm searching for a way to cd to real-dir with as less typing as possible.
You can use cd -P .
Note that this only updates the PWD and OLDPWD environment variables; the kernel-level current directory remains unchanged.
Alternatively, you can use the -P option with the initial cd like cd -P /var/virtual-dir.
You can:
cd "$(readlink -f .)"
If this is too much typing, you can create a helper function in your .bashrc, like this:
function cdlink() {
cd "$(readlink -f .)"
}
source ~/.bashrc or start a new shell and can simply type:
cdlink

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

Typed wrong mv 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
Hi i worked on Linux server , and was running this command mv matter/*/* .
but instead i have typed this mv matter /*/* .
because of which some errors starts coming on the screen , and then i was not able to login and when we reboot the server its not coming up.
so can you please tell me what this command has done mv matter /*/* .
You can find out for yourself by inserting an echo at the beginning of the command line:
echo mv matter /*/* .
The expanded command looks like this:
mv matter /bin/ash /bin/bash /bin/echo /bin/false [...] /home/yourname [...] .
All files and directories from the top-level directories (echo /*/) have been moved to this one directory where you executed that command. It's hard to separate them from there, but you can try using a rescue CD:
move all executable files to /bin
make /sbin a symlink to /bin
move all files that look like configuration files to /etc
But since you couldn't find out for yourself what the mv command was doing exactly, you should rather ask someone who knows to fix it for you. It's a lot of work, though.

UNIX: List files in directory with relative path [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
The question is:
What command would you use to list the text files in your
fileAsst directory (using a relative path)?
The previous question was:
Give a command to list the names of those text files, using an absolute path to the fileAsst directory as part of your command.
The answer was:
~/UnixCourse/fileAsst/*.txt
I was wondering how I can list the files in this directory using a relative path. I've tried several commands including:
ls ~/UnixCourse/fileAsst/*.txt|awk -F"/" '{print $NF}'
(cd ~/UnixCourse/fileAsst/*.txt && ls )
and a bunch of others.
But it keeps telling me their invalid. I know it has to be a correct answer because others have gotten past this. But right now I'm stuck and extremely frustrated =(
UPDATE:
After going to the CS lab someone helped me figure out the problem. I needed to be in a certain working directory at first, and I wasn't. After switching to that directory all I needed was the command:
../UnixCourse/fileAsst/*.txt
and that took care of it for me. Thanks to everyone that helped and I hope this helps someone else.
try:
$ cd ~/UnixCourse/fileAsst/
$ find .
as a one-liner (executing in a sub-shell)
$ (cd ~/UnixCourse/fileAsst/ && find .)
another approach
$ (cd ~/UnixCourse && ls fileAsst/*.txt
$ ls ~/UnixCourse/fileAsst/*.txt

Resources