Find + Exec: error paths must precede expression on, while excluding a directory [closed] - linux

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
So I wanted to do some find with exec commands in a recursive directory, while excluding one particular directory at a time, called admin/admin/design and admin/login/design, that have a symlink into it. Here are the snippet of the tree structure:
admin/
├── admin
│   └── design -> ../design
├── api
├── design
│   ├── 3rdparty
│ └── ...
├── error_pages
└── login
└── design -> ../design
Here is my current commands to it:
$ sudo su
$ find 'admin/admin/' -wholename 'admin/admin/design' -prune -o exec chattr -i -R {} \;
$ find 'admin/login/' -wholename 'admin/login/design' -prune -o exec chattr -i -R {} \;
And here is the message, which I think I got it wrong in the first place
find: paths must precede expression: exec
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
I've seen some answers that suggests to put quotes on the name path, but maybe it's not the case for this. Do you guys have any workaround for it? or maybe using other alternatives command?

Related

BEST Linux script; to rename SRT to name of movie file in same folder; multiple sub folders [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
There have been multiple attempts to answer this question, but no correct script can be found.
The problem:
SRT subtitles will not load unless having the same name as the movie, or same name as movie +.en.srt or .es.srt or .fr.srt and so on.
1000's of movie directories within a main movie directory having within their respective movie directory, sometimes 1+ .srt files (1_English.srt, 2_English.srt, *French.srt, etc.).
My media server is using Ubuntu, so the solution should use a BASH script.
Here is a snippet of my file structure:
Test-dir$ tree
.
├── renamer.sh
├── Saga.of.the.Phoenix.1990.1080p
│   ├── 1_French.srt
│   ├── 1_Spanish.srt
│   ├── 2_English.srt
│   ├── 3_English.srt
│   └── Saga.of.the.Phoenix.1990.1080p.BluRay.x265.mp4
├── Salt.and.Pepper.1968.1080p
│   ├── 1_French.srt
│   ├── 1_Spanish.srt
│   ├── 2_English.srt
│   ├── 4_English.srt
│   └── Salt.and.Pepper.1968.1080p.mp4
└── Salyut-7.2017.1080p.BluRay.x265
├── 2_English.srt
└── Salyut-7.2017.1080p.BluRay.x265.mp4
The questions:
In writing a BASH script,
There are multiple srt files with the same language, I usually like to choose the bigger file and remove the smaller file, the first part of script would have to sort same language srt and delete the smaller ones, how to script this?
How to change the name of srt's to have the same name as the movie file (not always mp4, sometimes mkv or avi.), while appending acronyms for language (en, es, fr, ru,..) if English.srt then change name to "MovieName".en.srt?
I have started the script removing srt files from the SUB directories of the movie directory and then deleting the SUB directory.
Also, added a script to delete any unwanted parts in the string of the movie, or delete unwanted files.
#!/bin/bash/
# Using current working DIR of where script is ran from
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Moves srt files from SUB folders to their movie folder.
for i in */Subs; do
mv "$i"/* "$i"/..
done
# Removes Subs directory.
find $DIR/* -type d -name "Subs" -exec rm -rf {} +
# Removing the additional rar string from the folders and their movie names.
find . -depth -name '*-rar*' -execdir bash -c 'for f; do mv -i "$f" "${f//-rar/}"; done' bash {} +
# Removing unwanted files from all movie folders.
find . -maxdepth 2 -type f \( -name "RAR.txt" -o -name "RAR.nfo" \) -delete
######## Your helper code starts from here to answer questions 1 and 2 #####################
Many thanks for helping with this conundrum, not only will this help one person, but many, on our quest to free many hours of copying, deleting, pasting, and all with a single script.
Update:
BTB91 gave a brilliant answer and has worked, however to help others learn the many ways to go about solving the same problem above I would like to keep this thread open.
IFS=$'\n' eval "MOVS=(\$( find \"\$DIR\" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' ))" # list of movies
for M in "${MOVS[#]}" ; do
cd "$DIR/$M"
IFS=$'\n' eval "LANGS=(\$( ls | sed -nr 's/.*_([[:alpha:]]+).srt/\1/p' | sort -u ))" # list of languages for movie
for L in "${LANGS[#]}" ; do
IFS=$'\n' eval "FILES=(\$( ls -S *_$L.srt))" # list files for language sorted by size
case "${L,,}" in
en*)
L=en
;;
sp*|es*)
L=es
;;
esac
mv -v "${FILES[0]}" "$M.$L.srt"
FILES[0]=
rm -vf "${FILES[#]}"
done
cd "$OLDPWD"
done
I used "IFS=$'\n' eval ..." because the directory or file names might contain spaces.

Copying file one location to another [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
below is mentioned code in shell script
SOURCE="/my/folder"
DESTINATION="/my/destination"
cp -r "$SOURCE/subdir/"* "$DESTINATION/another_sub/"
code is giving me an error
/my/folder: no such a file or directory
what is wrong with this code. I checked for folders also if I put directly on the terminal it is working but using shell script it is not working
/my/folder: no such a file or directory
This error indicates that it can't find that folder from root /.
Try
SOURCE="./my/folder"
DESTINATION="./my/destination"
if the script is run in the same directory as the folders. Or use
SOURCE="~/my/folder"
DESTINATION="~/my/destination"
if the folders are located off your user's home folder.
How to Copy a file from one Source location to another Destination location in Linux Operating System.
Follow below code & command as per your Source to the Destination location.
ubuntu#staging-docker:~/copyfile$ ls
copy.sh destination_dir source_dir
ubuntu#staging-docker:~/copyfile$ tree
.
├── copy.sh
├── destination_dir
└── source_dir
└── source_file.txt
2 directories, 2 files
ubuntu#staging-docker:~/copyfile$ cat copy.sh
#!/bin/bash
source_location='/home/ubuntu/copyfile/source_dir/source_file.txt'
Destination_location='/home/ubuntu/copyfile/destination_dir'
`cp -r $source_location $Destination_location`
ubuntu#staging-docker:~/copyfile$ sh copy.sh
ubuntu#staging-docker:~/copyfile$ tree
.
├── copy.sh
├── destination_dir
│   └── source_file.txt
└── source_dir
└── source_file.txt
2 directories, 3 files
ubuntu#staging-docker:~/copyfile$
ubuntu#staging-docker:~/copyfile$ mkdir source_dir
ubuntu#staging-docker:~/copyfile$ mkdir destination_dir
ubuntu#staging-docker:~/copyfile$ ls
destination_dir source_dir
ubuntu#staging-docker:~/copyfile$ cat > source_dir/source_file.txt
This is a source file
ubuntu#staging-docker:~/copyfile$ tree
.
├── destination_dir
└── source_dir
└── source_file.txt
2 directories, 1 file
ubuntu#staging-docker:~/copyfile$ cp source_dir/source_file.txt destination_dir/.
ubuntu#staging-docker:~/copyfile$ tree
.
├── destination_dir
│   └── source_file.txt
└── source_dir
└── source_file.txt
2 directories, 2 files
ubuntu#staging-docker:~/copyfile$

what's the difference between rm -rf and rm -r? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
In linux, what is the difference between 'rm -rf' and 'rm -r', both seem to do the same things (delete an entire directory).
Here is a few commands that I ran to test it:
mohammad#mohammad-ThinkPad-E570:~/testerr$ ls
mohammad#mohammad-ThinkPad-E570:~/testerr$ mkdir foo1 foo2
mohammad#mohammad-ThinkPad-E570:~/testerr$ touch foo1/main.java foo2/main.java
mohammad#mohammad-ThinkPad-E570:~/testerr$ tree
.
├── foo1
│   └── main.java
└── foo2
└── main.java
2 directories, 2 files
mohammad#mohammad-ThinkPad-E570:~/testerr$ rm -r foo1
mohammad#mohammad-ThinkPad-E570:~/testerr$ ls
foo2
mohammad#mohammad-ThinkPad-E570:~/testerr$ rm -rf foo2
mohammad#mohammad-ThinkPad-E570:~/testerr$ tree
.
0 directories, 0 files
mohammad#mohammad-ThinkPad-E570:~/testerr$
-f option is there to remove the prompts.
-r option is there to work recursively.
Let's say that we have a folder named stackoverflow with the contents of image.jpg otherimage.jpg mydog.doc
Upon typing rm -r stackoverflow terminal may say: rm: descend into write-protected directory 'stackoverflow'? and if you say y it will ask you for new questions.
rm: remove write-protected regular file stackoverflow/image.jpg'?
rm: remove write-protected regular file stackoverflow/otherimage.jpg'?
rm: remove write-protected regular file stackoverflow/mydog.doc'?
Basically, it will ask every step if you want to do this operation or not.
Now let's try with rm -rf stackoverflow
No questions will be asked this time and, all the content inside the folder is now deleted.
rm -rf ignores non-existent files, and never prompt before removing.
rm -r removes directories and their contents recursively.
https://www.computerhope.com/unix/urm.htm

How to find files which contain specific string in directory using command "find"? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
This is my directory structure.
dirOut
├── dirIn1
│   ├── temp1
│   └── temp2
├── dirIn2
└── dirIn3
├── temp1
├── temp2
├── temp3
└── temp4
dir is directory and temp is file.
I want to find files which contain specific string "Hello".
How do I use command "find" to find.
Use grep, not a find, when you find files base on the content.
grep -lr Hello .
-l : Normally grep print matching lines, with -l option, it just print the matched filenames.
-r : recursively find files under the directory.
find dirOut -type f -exec grep -l Hello {} +
The -l option tells grep to just list the filename if it finds a match, rather than showing all the matching lines.
You could also do it using the -R option to grep to search a directory recursively, rather than using find.
grep -R -l Hello dirOut
find . -type f -name \* -exec grep -l "hello" {} \;
Execute this command while in dirOut.

How can I list all the files in a directory and all its sub-directories? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I list all the files in a directory and all its sub-directories?
tree can accomplish this job:
$ tree
.
├── dir
│   └── f1
└── f2
1 directory, 2 files
But I want files to be listed in this format:
dir/f1
f2
Pass options -i and -f to tree:
tree -if
Option -i disables the printing of the indentation lines, option -f prints a path prefix for each file. This will however still list non-leaf directories.
Use the find command.
find . -type f
Apart from above mentioned solution, you can list the file in directory and its subdirectory using "ls" command with "-R" option.
ls -lR
One way to get this listing is using printf and ls -r
printf "%s\n" "$(ls -r)"

Resources