How to avoid error with ln command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to avoid error with ln command?
# 1  
Old 12-02-2016
How to avoid error with ln command?

Code:
mkdir logs
mkdir: Failed to make directory "logs"; File exists

To avoid this error i use the -p argument so it creates a folder only if it is does not exists like you see below.

Code:
mkdir -p logs

In the similar manner i wish to avoid this error with ln command

Code:
ln -s /tmp/myfolder var
ln: cannot create var: File exists

# 2  
Old 12-02-2016
You can test if it is a symlink.

Check out man test on your operating system.

Code:
test -h /path/mylink && echo "link" || echo "not link"

Regards.
# 3  
Old 12-02-2016
Sure this is a reasonable request? Which of the links should persist - the old one or the new one? Are you aware that overwriting the old value may destroy valuable information?

Read man ln for info on the -b, --backup, and -f options.
# 4  
Old 12-02-2016
I thought that the -p flag of mkdir was to make up any required paths, so if you issue mkdir /a/b/c/d and only /a/b exist, /a/b/c will be created for you.

My testing seems to suggest your way will still fail:-
Code:
$ touch /tmp/my-test
$ mkdir -p /tmp/my-test
mkdir: cannot create directory `/tmp/my-test': File exists
$

Am I missing something? It is always best to test before you try to create/replace something.


Robin
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 12-02-2016
They are not equivalent. mkdir always does the same thing - create an empty folder - while the entries ln creates depend on the source.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Avoid running unnecessary repetitive ps command

i have the following code: APIDS=$(echo $(ps -ef | awk -v gpid="${gpid}" '$2 == gpid || $3 == gpid {print $2,$3}') | sed 's~ ~|~g') AllProcs=$(ps -ef | awk -v allpids="${APIDS}" '$2 ~ allpids || $3 ~ allpids {print $0}' | sed '/^$/d') it seems the above... (6 Replies)
Discussion started by: SkySmart
6 Replies

2. UNIX for Beginners Questions & Answers

How to avoid arguments inside Nawk command?

Hi, Here is my command print $2 was meant to select the second column however, it is getting substituted with the second argument that was passed to the script. Can you please tell me how can I resolve this ? (6 Replies)
Discussion started by: mohtashims
6 Replies

3. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

4. Shell Programming and Scripting

How to avoid ISQL error?

I am using ISQL command in my script like below echo "going to execute isql for getting JE count" isql -Uxyz -SSYB_LOCOMOTIVE_xx -Pabc -Dlmn -w300 -o$sumOfJEOutputFile <<EOF select sum(count(*)) from lmn..ex_je_t where clm_id in ($(cat $finalTemp)) group by clm_id go EOF retval=$? cat... (2 Replies)
Discussion started by: Sharma331
2 Replies

5. Shell Programming and Scripting

avoid error on terminal window!

Hi Guys, I am using simple ls command to find out whether the dir exist there or not. If dir found than remove. But the problem is I don't want the error shown on the terminal window that "dir not found" on the terminal window everytime i execute it. The code I am using is: set x = `/bin/ls... (4 Replies)
Discussion started by: dixits
4 Replies

6. UNIX for Dummies Questions & Answers

how to avoid time command output

Hi, I have 2 queries 1 .when I run some unix command, I am getting the output of "time" at std output (screen) for eg zegrep <pattern> *.v.gz I almost found the reason but not sure, if the no of files matching *.v.gz is more then I am getting the time command output at the... (5 Replies)
Discussion started by: selvaka
5 Replies

7. Shell Programming and Scripting

Need to avoid empty line error

Hi, Below shell script executes based on liasted data files parameters.But small problem need to avoid ,If any empty line occures in dat files it's throwing oracle error .Need to ignore empty lines (means does not ready by script).Please advice. #/bin/sh adsts=`cat... (2 Replies)
Discussion started by: krajasekhar.v
2 Replies

8. Shell Programming and Scripting

awk command to avoid loops

Hi... I need a help in using the awk command or any other solution to avoid the usage of loops. My question is : I have a input like this : field1|field2|field3|field4|field5|field6|field7|field8|field9 ex : 4000|testing|scenario|14450|500|320|450|200|100 where the... (2 Replies)
Discussion started by: vijayarajvp
2 Replies

9. UNIX for Dummies Questions & Answers

How to avoid historying my command

Dear all, Normally unix automatically record up to 500 the command lines whatever I put in. Does anyone knows how I can avoid this record, in another word, I dont want system remember what I typed in thanks (2 Replies)
Discussion started by: ting123
2 Replies

10. Shell Programming and Scripting

Can I avoid the standard output from kill command

I am sending a kill comand to kill a process inside a SH script but I don`t want the user to notice it so I donīt want the message "1222 killed" to appear. I`ve tried to redirect the standard output to /dev/null 2>&1 and also tried to use "nohup" but none of them was succesfull. Can anyone... (1 Reply)
Discussion started by: pguinal
1 Replies
Login or Register to Ask a Question