Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
03-16-2017, 03:54 PM
|
#1
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Rep: 
|
creating symlinks except
Hi Folks,
I have a little script:
Code:
#!/bin/sh
SD="/var/spool/storage/SD_DISK/"
ARRANGED_FOLDER="/var/spool/storage/SD_DISK/videos_by_day"
for i in $(find "$SD" -type f); do
DAY=$(echo "$i" | cut -d'/' -f10 | cut -d'_' -f1 | sort | uniq)
if [ ! -d "/$ARRANGED_FOLDER/$DAY" ]; then
mkdir -p /"$ARRANGED_FOLDER"/"$DAY"
fi
cd "$ARRANGED_FOLDER"/"$DAY"
if ! ln -sfn "$i"; then
echo "[-] Error: Could not create symbolic links"
fi
done
I'd like to jump and not create symbolic links of any xml files. How can I do that? Please help me a bit
|
|
|
03-16-2017, 04:29 PM
|
#2
|
Senior Member
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,950
|
You could try
Code:
file some_thing_that_is_an_xml_file
This should reply with 'some_thing_that_is_an_xml_file: XML document text' if true.
|
|
|
03-17-2017, 01:38 AM
|
#3
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep: 
|
Done! I modified the for cycle a bit:
Code:
for i in $(find "$SD" -type f -name "*.mkv"); do
this way it only searches for the intended mkv files, and exludes xmls.
Thanks though
|
|
|
03-17-2017, 01:45 AM
|
#4
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep: 
|
When I rerun the script, it starts to create all symbolic links again.
Could someone tell me how can I jump, and not recreate symbolic links which are already created,
and only create when there is a new mkv file which dowsn't have a symlink yet.
I'm worried a bit about that there would be to much write on the SD card.
|
|
|
03-17-2017, 05:16 AM
|
#5
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,979
|
so you need to add a check if that link already exists (before ln -sfn)
|
|
1 members found this post helpful.
|
03-17-2017, 09:27 AM
|
#6
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Code:
SD="/var/spool/storage/SD_DISK/"
ARRANGED_FOLDER="/var/spool/storage/SD_DISK/videos_by_day"
while read FILENAME
do
c=$FILENAME
xpath=${c%/*}
xbase=${c##*/}
xfext=${xbase##*.}
xpref=${xbase%.*}
path=${xpath}
fname=${xpref}
ext=${xfext}
ckfile="$fname"."$ext"
DAY=$(echo "$FILENAME" | cut -d'/' -f10 | cut -d'_' -f1 | sort | uniq)
DailyFiles="$ARRANGED_FOLDER"/"$DAY"
mkdir -pv "$DailyFiles"
# check to see if in directory already / which it should not be?
if [[ ! "$DailyFiles"/"$ckfile" ]] && ln -sfn $FILENAME "$DailyFiles"/"$ckfile"
done << (find "$SD" -type f -name "*.mkv")
The idea is to check to see if a linked file is not in the dir already by same name, if true then create one. (not tested).
Last edited by BW-userx; 03-17-2017 at 09:30 AM.
|
|
|
03-17-2017, 09:44 AM
|
#7
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
rethinking this. You just want to link movies to a different directory created each time you run this?
in other words:
Each time you run this is WILL be on a different batch of files picking out all of the movies, creating a separate dir for each movie file by its own file name then linking that file into that same dir?
Code:
SD="/var/spool/storage/SD_DISK/"
ARRANGED_FOLDER="/var/spool/storage/SD_DISK/videos_by_day"
while read FILENAME
do
c=$FILENAME
xpath=${c%/*}
xbase=${c##*/}
xfext=${xbase##*.}
xpref=${xbase%.*}
path=${xpath}
fname=${xpref}
ext=${xfext}
LinkedFile="$fname"
DailyFiles="$ARRANGED_FOLDER"/"$LinkedFile"
mkdir -pv "$DailyFiles"
ln -s $FILENAME "$DailyFiles"
done << (find "$SD" -type f -name "*.mkv")
NOT TESTED
bonus link
http://stackoverflow.com/questions/8...broken-in-bash
Last edited by BW-userx; 03-17-2017 at 10:11 AM.
|
|
1 members found this post helpful.
|
03-18-2017, 01:51 AM
|
#8
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep: 
|
I was given a hint. That I should only make symlinks of only those files that doesn't have yet.
But that means a comparisson.
readlink is available on the product's embedded linux.
here I can make a list of those symlinks that have an original file
for j in $(find "$ARRANGED_FOLDER" -type f -name "*.mkv"); do
readlink $j
done
Ho could I make a list and create symlinks only of those that doesn't have yet.
Last edited by kzo81; 03-18-2017 at 02:35 AM.
|
|
|
03-18-2017, 02:32 AM
|
#9
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep: 
|
Quote:
Originally Posted by BW-userx
rethinking this. You just want to link movies to a different directory created each time you run this?
in other words:
[code]
c=$FILENAME
xpath=${c%/*}
xbase=${c##*/}
xfext=${xbase##*.}
xpref=${xbase%.*}
path=${xpath}
fname=${xpref}
ext=${xfext}
|
Hi, thanks for helping me, but I don't understand that code segment
|
|
|
03-18-2017, 02:52 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,979
|
Quote:
Originally Posted by kzo81
I was given a hint. That I should only make symlinks of only those files that doesn't have yet.
|
that was post #5
Quote:
Originally Posted by kzo81
readlink is available on the product's embedded linux.
|
readlink is not the tool you need, please read at least the man page of it: https://linux.die.net/man/1/readlink
Quote:
Originally Posted by kzo81
for j in $(find "$ARRANGED_FOLDER" -type f -name "*.mkv"); do
readlink $j
done
|
do not use this construct, for j in $(find ...) is not safe. You can use the while loop, see post #6:
Code:
while read filename
do
....
done << (find "$SD" -type f -name "*.mkv")
Quote:
Originally Posted by kzo81
Ho could I make a list and create symlinks only of those that doesn't have yet.
|
You do not need to create that list, but add a simple check (in your original script) to see if that link was already created. (see post #5 again)
BW-userx posted an incorrect solution:
Code:
if [[ ! "$DailyFiles"/"$ckfile" ]] && ln -sfn $FILENAME "$DailyFiles"/"$ckfile"
you can do something similar, like this:
Code:
[[ -h "$DailyFiles/$ckfile" ]] || ln -sfn $FILENAME "$DailyFiles/$ckfile"
finally about post #9: see man bash, look for parameter expansion, especially check ## and %%:
Code:
${parameter#word}
${parameter##word}
Remove matching prefix pattern.
${parameter%word}
${parameter%%word}
Remove matching suffix pattern.
|
|
1 members found this post helpful.
|
03-18-2017, 06:15 AM
|
#11
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep: 
|
I've read readlink man page and now I know it's not going to solve this. Thanks.
I'll check parameter expansion
I dont understand other syntaxes, could I achieve the same result with my thought?
now the code looks like this:
Code:
#!/bin/sh
echo "[+] Creating symbolic links ..."
SD="/var/spool/storage/SD_DISK/"
ARRANGED_FOLDER="$SD/videos_by_day"
while read i
do
DAY=$(echo "$i" | cut -d'/' -f10 | cut -d'_' -f1 | sort | uniq)
if [ ! -d "/$ARRANGED_FOLDER/$DAY" ]; then
mkdir -p /"$ARRANGED_FOLDER"/"$DAY"
fi
cd "$ARRANGED_FOLDER"/"$DAY" || exit 1
if [ here I need a way to check if there is a link already created ]
fi
elif ! ln -sfn "$i"; then
echo "[-] Error: Could not create symbolic links"
fi
done << (find "$SD" -type f -name "*.mkv")
Last edited by kzo81; 03-18-2017 at 06:18 AM.
|
|
|
03-18-2017, 06:23 AM
|
#12
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,979
|
yes, more or less yes (fi not required)
|
|
|
03-18-2017, 08:16 AM
|
#13
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by kzo81
Hi, thanks for helping me, but I don't understand that code segment
|
of course you don't. Look up string manipulation.
Code:
echo "$xpath"
echo "$xbase"
etc..
Basically just echo all of them to see what it is doing. If you check that link it gives you good insight on how to check for links.
|
|
1 members found this post helpful.
|
03-18-2017, 08:34 AM
|
#14
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by pan64
BW-userx posted an incorrect solution:
Code:
if [[ ! "$DailyFiles"/"$ckfile" ]] && ln -sfn $FILENAME "$DailyFiles"/"$ckfile"
|
I beg to differ - YO. A link to a file shows what in the other directory it is link into? A file. As if it is already there. Hench the reason for soft links.
looks like he is only doing "links to" therefore no real fancy checks need to be done here.
All you have to so is look for that file first which is actually a link from another file. So if it is not there then create just a link to it within that same directory. done.
think about it. the only thing wrong is that code is the 'if' part. Motor reflects, or "force of habit" as some say it. I am use to doing complete if statements just not them ones. but the logic works and so does that code after removing the 'if' and adding the '-f'
Code:
[[ ! -f "$DailyFiles"/"$ckfile" ]] && ln -sfn $FILENAME "$DailyFiles"/"$ckfile"
as stated it was not tested. But now it is
Last edited by BW-userx; 03-18-2017 at 09:06 AM.
|
|
|
03-18-2017, 09:15 AM
|
#15
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
@kzo81
this returns me nothing. - $DAY
Code:
DAY=$(echo "$i" | cut -d'/' -f10 | cut -d'_' -f1 | sort | uniq)
is that "suppose" to give you a file name? because it comes up empty when I run it. just run this simple version with a path you give it to search. any path will work it is not changing any data.
Code:
#!/bin/bash
working_dir="/home/userx/test"
while read i
do
DAY=$(echo "$i" | cut -d'/' -f10 | cut -d'_' -f1 | sort | uniq)
echo "$DAY"
done < <(find "$working_dir" -type f -name "*.*")
gives me empty results. if you are just looking for the file name then that code I wrote that you do not understand gives you that. a file name to check
Last edited by BW-userx; 03-18-2017 at 09:32 AM.
|
|
|
All times are GMT -5. The time now is 04:26 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|