简体   繁体   中英

How do I make a symbolic link to list of directories?

I must move my $HOME/.directories because I have wrong partitioning.

I moved to a another partition with much more free space. But I wanna make a symbolic links, but I cant. Because when I try something like:

for file in `ls -la | awk '{print$09}'`; do sudo ln -sf "$PWD/$file" /home/inukaze/$file; done

usage: sudo [-D level] -h | -K | -k | -V
usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid]
usage: sudo -l[l] [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-U user name] [-u user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-C fd] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s]
            [<command>]
usage: sudo -e [-AknS] [-C fd] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid] file ...

I need the full path for correct read of symbolic links. And I this is not working. How I can make a list of folders begins with “dot” ( . ), and make symbolic links inside my home directory?

Okey friends i have try with :

1 :

for file in `ls -la "$PWD" | awk '{print$09}' | grep "^\."` ; do `sudo ln -sf $("$PWD/$file") $"("/home/inukaze/$file")"` ; done

2 :

MYDIR="/media/Compartido/.linux/Home/Inukaze"
DIRS=`ls -la "$MYDIR" | egrep '^d' | awk '{print $9}'`
for DIR in $DIRS ; do sudo ln -sf "$PWD/$DIRS" "/home/inukaze/$DIRS" ; done

3 :

find . -type d -maxdepth 1 |while read DIRNAME; do sudo ln -s "${DIRNAME}" /home/inukaze/"${DIRNAME}"; done

But with the Three Methods , i got the same result , much bad symbolic links and just 6 are right . i dont understand why.

something are wrong , i start to think is in the

"sudo ln -s $Varible" $Destiny ; done"

well . another ideas ???

shopt -s dotglob
cp -s * ~inukaze

Hmmm. Not 100% clear what you are trying to achieve or what it's not working, but I wrote this to attempt to debug based on your sample code. Note I am using echo instead of ln -s for now as well as changing file to the uppercase FILE and wrapping in brackets as well as double quotes:

for FILE in `ls -la | awk '{print$09}'`;
do
  echo "${PWD}/${FILE}" /home/inukaze/"${FILE}"
done

And on my local system—Mac OS X 10.9—it outputs:

/Users/jake/.bash_history /home/inukaze/.bash_history
/Users/jake/.bashrc /home/inukaze/.bashrc
/Users/jake/.cache /home/inukaze/.cache
/Users/jake/.gitk /home/inukaze/.gitk
/Users/jake/.htoprc /home/inukaze/.htoprc
/Users/jake/.mysql_history /home/inukaze/.mysql_history
etc…

So unclear how your script is not working? Do you only want directories? Or files?

EDIT: Original poster is looking to only symlink directories. So try this using find instead:

find . -type d -maxdepth 1 |\
  while read DIRNAME
  do
    echo "${DIRNAME}" /home/inukaze/"${DIRNAME}"
  done

In my case, it finds all directories in my current home directory like this and echoes the /home/inukaze/"${DIRNAME} right after that:

./.ssh /home/inukaze/./.ssh
./.subversion /home/inukaze/./.subversion
./.Trash /home/inukaze/./.Trash
./.Xcode /home/inukaze/./.Xcode
./Applications /home/inukaze/./Applications
./Desktop /home/inukaze/./Desktop
./Documents /home/inukaze/./Documents
etc…

In this example I am just using echo to test the overall logic. And that is using find with the -type d option which tells it to just find directories and -maxdepth 1 which is connected to the maximum depth find should go. Doing 1 deep will not explore any directory deeper than the directory you are in.

how i can make a list of folders begins with "dot" (.)

shopt -s dotglob
echo .*/ # This glob matches only dot-directories; however that includes ./ and ../

and make symbolic links inside my home directory?

shopt -s dotglob
for d in "$PWD"/.*/; do
  case "$d" in
    *./|*../) :;; # do nothing for these two directories
    *) ( cd && ln -s "$d" );;
  esac
done

The above is more verbose than Ignacio Vazquez-Abrams' answer , but also links only dot directories other than . and .. . It also will work on BSD/OSX.

I think the problem is with

ls -la | awk '{print$09}'

Because with this all folder finish with "/" and the "ln -sf" not create a symbolic links if you add / in the finish

i try with

ls -la | awk '{print$09}' | sed 's/\\/*$//

just for remove the last character in ls -la

Okey i think i solved

i go to my backup folder , in this case /media/Compartido/.linux/Home/Inukaze

$ for file in $(ls -la | awk '{print$09}' | sed 's/\\/*$//') ; do sudo ln -sf "$PWD/$file" "/home/inukaze/$file" ; done

ln: <</home/inukaze/./.>> : cannot overwrite a directory

ln: <</home/inukaze/../..>> : cannot overwrite a directory


and ready all symbolic links are correctly created :=)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM