I have a list file of selected pdfs (each line contains a path to pdf). These pdfs are located in different directories which are several thousands in total. I am very new to using bash and struggling to open only those files which are listed in the list file. Any help would be much appreciated.
1 Answer
If there are thousands of files listed, you probably don't want to open them all at once. This little script opens the one you specify on the command line by number. You could modify it to print out the name or search the list, etc.
#!/bin/sh case "$1" in [1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]) N="$1" ;; *) echo "Put a file number on the command line"; exit ;; esac while read pdfpath; do if [ $((--N)) = 0 ]; then atril "$pdfpath" & # Or evince, okular, etc. exit fi done < listfile.txt The opening case sets N to the first command line argument ($1) if it's a number but exits if a number wasn't specified.
while read ... done loops through the lines in your list file and reads each line in turn in the variable pdfpath.
$((--N)) decrements the variable, so when it gets to zero it matches the comparison and enters the if clause.
The & causes the command to execute but control returns immediately so the script can exit and not tie up your bash prompt.