[SOLVED]
Bing ChatGPT is THE sh*t man! It got this right in a few secs!!! WOW! Thank you to everyone who answered me, it's due to your help that I was able to solve it through Bing. But holy F, am I impressed with Bing!
I hate MS and I hated bing,BUT they have got something real good here.
find /home/$USER -maxdepth 1 -type f -executable -exec sh -c 'head -n 1 "$1" | grep -q "^#!/bin/bash" && cp "$1" /home/bob/Documents/Linux/Regularly_Copied_files_crontab' sh {} \;
edit: I just want to copy scripts in /home/$USER
folder, not all the other subfolders.
edit 2: I think the better approach here would be to have two conditions.
- The file is in /home/$USER/ and not in it's subfolders.
- The file's first line should be
I don't actually need all executable files, I just want my bash scripts, but unfortunately, I don't have the good habit of giving the .sh extensions to all of them. These files are all executable, they all have a shebang line (`#!/bin/bash) as their first line, how can I copy them elsewhere? I mean, I know how the copy commands work, but I don't know if I can specify the pattern here.
How would I specify a cp
command to only copy bash scripts
to my docs folder
?
Intended Use case: I am trying to create a command to copy all the bash scripts
I have created in my home folder to my Documents
folder. My docs folder is synced everyday, so I won't ever lose my scripts as they would be stored in the cloud.
find ~ -type f -executable
find /home/user -type f -perm /u+x -not -path "/home/user/Documents" -exec cp {} ~/Documents ;
___
Is using another script okay?
#!/bin/bash docs_folder=/some/folder cd files=(*) for f in "${files[@]}"; do [[ "$(cat "$f" | head -n 1)" = '#!/bin/bash' ]] && cp "$f" "$docs_folder/$f" done
It's fucking up the ampersands for some reason.
it's an unfixed bug. Pretty damning. I am sorry, but I got the answer. Thank you for trying to help me anyway.