Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to create a script in Perl that automates some stuff for me.
But let me start with another question.
When do I do the following:
Code:
$ ls -haltr
total 1.7G
lrwxrwxrwx 1 user user 24 Feb 28 15:04 file.xml -> /path/to/file/file.xml
I see that my 'file.xml' is not physically present, it's linked to another file somewhere else.
The question is, is there a way to 'grep' that part and use it in a script?
What I eventually want to do is to copy that file to the local directory and change the name to the original file.
Example:
file is called test.xml
schortcut is like /path/to/file/blablabla.doc
So I want to copy the blablabla.doc file to my local directory, then remove the original test.xml and finally rename the copied blablabla.doc to test.xml (so that I don't have 2 test.xml files).
Yeah, I know so I will make sure to use some checks like 'Are you sure to delete <filename>?'.
But it's still a work in progress and it's the first time that I will be using Perl so a lot of trial and error will be involved
lrwxrwxrwx 1 user user 24 Feb 28 15:04 test.xml -> /path/to/file/file.xml
Then in your script, you can:
Code:
cp -p /path/to/file/file.xml /path/to/local_dir
unlink test.xml
mv -i /path/to/local_dir/file.xml /path/to/local_dir/test.xml # Option -i will make mv interactive, so it will ask you before rename
pan64's code is not much tough. It can simply be understood, if you've tried:
Code:
FILE=<local filename> # Saving local finename in FILE variable
[[ -L "$FILE" ]] && { # Testing if file FILE exists and is a link file
FILE2=`ls -1ld "$FILE" | awk ' { print $NF } ' ` # Saving link name in FILE2 variable
rm "$FILE" # Removing file i.e. link
rm -i "$FILE" # Removing file i.e. link interactively
cp "$FILE2" "$FILE" # Renaming link
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.