LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 03-16-2017, 03:54 PM   #1
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Rep: Reputation: Disabled
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
 
Old 03-16-2017, 04:29 PM   #2
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
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.
 
Old 03-17-2017, 01:38 AM   #3
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
Wink

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
 
Old 03-17-2017, 01:45 AM   #4
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-17-2017, 05:16 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,847

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
so you need to add a check if that link already exists (before ln -sfn)
 
1 members found this post helpful.
Old 03-17-2017, 09:27 AM   #6
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.
 
Old 03-17-2017, 09:44 AM   #7
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.
Old 03-18-2017, 01:51 AM   #8
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-18-2017, 02:32 AM   #9
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
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
 
Old 03-18-2017, 02:52 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,847

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by kzo81 View Post
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 View Post
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 View Post
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 View Post
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.
Old 03-18-2017, 06:15 AM   #11
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-18-2017, 06:23 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,847

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
yes, more or less yes (fi not required)
 
Old 03-18-2017, 08:16 AM   #13
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by kzo81 View Post
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.
Old 03-18-2017, 08:34 AM   #14
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by pan64 View Post
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.
 
Old 03-18-2017, 09:15 AM   #15
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
@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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any default numbering system for creating symlinks inside /etc/rc3.d/ krssarath Fedora 4 02-09-2013 02:38 PM
LXer: Creating Symlinks How and Why LXer Syndicated Linux News 2 08-03-2012 06:13 AM
[SOLVED] About creating symlinks in .SlackBuild and doinst.sh cigerma Slackware 7 12-31-2011 06:31 AM
Creating symlinks on a CIFS share? MasterC Linux - General 2 08-30-2007 04:34 PM
Creating symlinks bripage Linux - General 2 07-23-2002 07:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration