-2

I am a computer science student. Second year of school - and I'm learning about Linux. Because of the Corona virus, I study in an independent way. During my studies I came across a question with a number of sections - I can't solve it.

The question wants me to execute the mv command - that I transfer some files to the folder. I tried to execute the mv command - and tried to integrate "pipe" or other commands like find.

This is not a normal mv command in my opinion, more things need to be integrated, how I can't decipher it. I tried to solve the question for a few days. I also tried to find such identical questions in the forum - but I couldn't find them.

The question:

  1. Write one command that will copy all files ending in .c or .h into the Programs folder.

My try was maybe do that:

find "*.c" -o "*.h" | mv Programs
9
  • Welcome @Haham to Unix SE. As a new user, please make yourself familar with the specific Q&A style of this site. You may start with this unix.stackexchange.com/help/how-to-ask
    – pLumo
    Apr 7, 2020 at 8:17
  • My question is not good?
    – hah
    Apr 7, 2020 at 8:19
  • Unfortunately not. (1) It is 3 questions. (2) You don't show what you tried and how that failed. Actually, your task is relatively easy and you will find answers on this site when you search.
    – pLumo
    Apr 7, 2020 at 8:21
  • 1
    I'll fix it right away
    – hah
    Apr 7, 2020 at 8:22
  • 2
    Does this answer your question? copying files with particular names to another folder
    – muru
    Apr 7, 2020 at 8:34

1 Answer 1

2

The assignment can be interpreted in three ways:

  1. Copy all .c and .h files from the current directory to the directory Programs, or
  2. Copy all .c and .h files from the current directory or anywhere below it to the directory Programs.
  3. Copy all accessible .c and .h from anywhere on the system to the directory Programs.

Note that the assignment says "copy", not "move". This means that you should be using the cp command, not mv.

The first interpretation of the assignment is solved by

cp *.[ch] Programs

(assuming that *.[ch] matches all files we're interested in, and there's not many thousands of these in the current directory).

The second interpretation of the assignment is solved by

find . -path ./Programs -prune -o -type f -name '*.[ch]' -exec cp {} Programs \;

This searches for all regular files with a .c or .h filename suffix in or below the current directory, and executes cp for each such file. We can't pipe to cp since that utility does not read its standard input stream.

We also make sure to avoid looking inside the Programs directory (assumed to be a subdirectory in the current directory). Not doing that would end up copying files from Programs into Programs. See "Explain find's -path and -prune options" for more info about -path and -prune.

The third interpretation is easy to implement from the second:

find / -path "$PWD/Programs" -prune -o -type f -name '*.[ch]' -exec cp {} Programs \;

... we just need find to start at the top of the directory tree and to specify that the Programs directory to avoid is the one at the path $PWD/Programs.

This will likely throw a number of errors at you due to you not having access everywhere. To avoid this, also prune directories that you can't access and only copy files that you can read:

find / \( -type d ! -executable -o -path "$PWD/Programs" \) -prune -o \
    -type f -name '*.[ch]' -readable -exec cp {} Programs \;

This requires GNU find for the -executable test (a directory is searchable if it's executable) and the -readable test.

Note that the find variations here will restrict the files to only regular files (through the -type f test), while the cp *.[ch] approach would copy any type of thing that ends in those two characters. Also, the find solution would copy hidden files (files with names that begin with a dot), while the first solution would not match those types of names by default.

1
  • that was helpful
    – hah
    Apr 7, 2020 at 9:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .