this post was submitted on 04 Oct 2023
22 points (89.3% liked)

Linux

48081 readers
765 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

[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.

  1. The file is in /home/$USER/ and not in it's subfolders.
  2. The file's first line should be #!/bin/bash

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 ;

___

all 16 comments
sorted by: hot top controversial new old
[–] IsoKiero@sopuli.xyz 7 points 1 year ago

find /home/user -type f -perm /u+x -not -path "/home/user/Documents" -exec cp {} ~/Documents \;

Run it without exec -parameter to get a list of files affected, I'd guess that that will catch more than you want as it only checks that it's a normal file and has the excecutable -bit on. To get only bash-scripts you'd first need to get a list of files with find and then check if it's a script with something (grep or maybe file should work) and copy based on that result, but it shouldn't be too difficult to write a script for it.

[–] neoney@lemmy.neoney.dev 4 points 1 year ago (1 children)

find ~ -type f -executable

will give you a list of all executable files You can probably use find’s -exec to move them all.

[–] Synthead@lemmy.world 2 points 1 year ago

This is the right answer. OP got quite the command from Bing 😉

[–] Astigma@feddit.uk 4 points 1 year ago (1 children)

grep -rl '#!/bin/bash' . | xargs -I {} cp {} /path/to/destination/

[–] Subject6051@lemmy.ml 1 points 1 year ago (1 children)

One problem, I think it's copying all the files inside folder /home/$USER whereas I just want the bash files which are in /home/$USER exlcluding all the subfolders. The command grep -rl '#!/bin/bash' . | xargs -I {} cp {} /path/to/destination/ won't exclude subfolders.

[–] TankieTanuki@hexbear.net 2 points 1 year ago* (last edited 1 year ago) (1 children)

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.

[–] Subject6051@lemmy.ml 2 points 1 year ago

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.

[–] hatedbad@lemmy.sdf.org 2 points 1 year ago* (last edited 1 year ago)

assuming you have a GNU toolchain you can use the find command like so:

find . -type f -executable -exec sh -c '
case $( file "$1" ) in (*Bourne-Again*) exit 0; esac
exit 1' sh {} \; -print0 | xargs -0 -I{} cp {} target/

This first finds all executable files in the current directory (change the “.” arg in find to search other dirs), uses the file command to test if it’s a bash file, and if it is, pipes the file name to xargs which calls cp on each file.

note: if “target” is inside the search directory you’ll get output from cp that it skipped copying identical files. this is because find will find them a free you copy them so be careful!

note 2: this doesn’t preserve the directory structure of the files, so if your scripts are nested and might have duplicate names, you’ll get errors.

[–] lem@lemmy.world 1 points 1 year ago* (last edited 1 year ago) (1 children)

identify

find DIR -executable

copy

find DIR -executable -exec cp {} TARGETDIR \;

[–] Subject6051@lemmy.ml 1 points 1 year ago (1 children)

find DIR -executable

unfortunately, this would list all the executables. I only want the executables in /home/$USER not it's subfolders

[–] FuckyWucky@hexbear.net 1 points 1 year ago* (last edited 1 year ago)

find ~/ -type f -maxdepth 1 -exec sh -c 'head -n 1 "$0" | grep -q "^#!/bin/bash$"' {} ; -exec cp {} . ;

i tried and it seemed to work

[–] Maoo@hexbear.net -3 points 1 year ago

Combine grep -l to find files with the shebang and cp to copy them to your docs folder. You can one-liner pipe or save the grep results to file and iterate. If the directories are nested and some of these files have the same name, they'll conflict if you don't include most of the original path in the cp target.

If you need them to be executable I think I'd use find first.