'How to move list of files from different directories to new directory using bash

I used find ./busd/ -type f -iname '*.pdf' | less to look for .pdf files on my second drive and there are hundreds of results from different subdirectories but I only needed to copy/move selection of pdf files to new directory.

I copy pasted their paths/filenames to moveme.txt.

I am thinking of cat moveme.txt | xargs command but i am stocked there. Please help.

I tried cat moveme.txt | xargs -0 -I % sh -c 'sudo mv -v % /new/directory/' and failed. How to do this right?

edit: I like to add that this path/filenames have spaces. Maybe it matters.

excerpt from moveme.txt:

./busd/0128/csm/sorting/read-ables/linus/CLI/Bash-Beginners-Guide-new.pdf
./busd/0128/csm/3t2/readables/etc/xulin/Shell Scripting.pdf
./busd/0128/csm/dais6/Dearables/assorted/Bash Pocket Reference - Arnold Robbins.pdf


Solution 1:[1]

find ./busd/ -type f -iname '*.pdf' -print0 |
  xargs -0 -P0 sh -c 'mv -v "$@" /new/directory/' sh

Explanation

  1. It makes little sense to pipe stdout of find to xargs -0 without -print0. The find command requires -print0 option because filenames might have spaces as you mentioned.
  2. Using xargs -I% would not benefit from the parallelization feature of xargs -P NUMBER. xargs -P0 would utilize all CPU cores.
  3. The last sh of sh -c '...' sh is required for $0. It can be arbitrary string but you might want to make a sensible name.

DIY

seq 100000 | sed 's/$/.pdf/' | xargs -P0 touch
mkdir -p new/directory
find . -type f -iname '*.pdf' -print0 |
  xargs -0 -P0 sh -c 'mv -v "$@" new/directory/' sh
  1. Remove -P0 and see the difference of time costing.
  2. Try xargs -p or xargs -t to see how many arguments xargs passes.
  3. Try sh -c 'ruby -e "p ARGV" mv -v "$@" new/directory/' for debugging. You can use any language you prefer, take node for example: sh -c 'node -e "console.log(process.argv.slice(1))" mv -v "$@" new/directory/'

Solution 2:[2]

as an option, it can be done in one command:

find ./busd/ -type f -iname '*.pdf' -exec mv -t /target_directory/ {} +

or

find ./busd/ -type f -iname '*.pdf' -print -exec mv -t /target_directory/ {} +

to print all copying files to terminal

Solution 3:[3]

Are you trying to collect numeric chars?

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
lst = [el for el in msg if el.isnumeric()]

Solution 4:[4]

If you want to store all the int elements of your string in your lst array, with your code the simplest way to do that :

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
lst = []
for i in range(len(msg)):
  if (msg[i].isdigit()):
      lst.append(msg[i]) #converted string msg to list called lst
print(lst)

Solution 5:[5]

you could check each char of the string and test if the value is a digit, using the any function you can stop checking once you find the first digit in the string if all you need to know if the string has digits

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
msg1 = "this has no ints"
print(f"contains ints: {any((char.isdigit() for char in msg))}")
print(f"contains ints: {any((char.isdigit() for char in msg1))}")

OUTPUT

contains ints: True
contains ints: False

Solution 6:[6]

lst = [(int if c.isnumeric() else str)(c) for c in msg]

Of course, it doesn't follow best practices but it's nice and short. Converts the character to an integer if it's numeric.

Solution 7:[7]

After making sure it's a digit, you append the value as int

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
lst = []
for i in range(len(msg)):
    if (msg[i].isdigit()):
        lst.append(int(msg[i]))
    else:
        lst.append(msg[i])
print(lst)

output:

['M', 'e', 'e', 't', ' ', 'm', 'e', ' ', 'a', 't', ' ', 't', 'h', 'e', ' ', 'R', 'i', 't', 'z', ' ', 'C', 'a', 'r', 'l', 't', 'o', 'n', ' ', 'a', 't', ' ', 9, ' ', 'o', "'", 'c', 'l', 'o', 'c', 'k', ',', ' ', 'd', 'o', 'n', "'", 't', ' ', 'b', 'e', ' ', 'l', 'a', 't', 'e', '!']

Solution 8:[8]

Case 1.1

If you want to extract the words from the string given, This one makes a list of words from the given string :

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
res = msg.split()
print ( str(res) )

Output:

['Meet', 'me', 'at', 'the', 'Ritz', 'Carlton', 'at', '9', "o'clock,", "don't", 'be', 'late!']

Case 1.2

If you want to extract the words from the string given, This one makes a list of words from the given string but it stores the numeric values as integer:

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
res = msg.split()
for index, item in enumerate(res):
    if item.isnumeric():
        res[index] = int(item)
        
print ( str(res) )

Output: (notice the number is stored as a int here, not as a string like the previous one)

['Meet', 'me', 'at', 'the', 'Ritz', 'Carlton', 'at', 9, "o'clock,", "don't", 'be', 'late!']

Case 2.1

If you want to list each of the characters from the string given, use this

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
print(list(msg))

Output:

['M', 'e', 'e', 't', ' ', 'm', 'e', ' ', 'a', 't', ' ', 't', 'h', 'e', ' ', 'R', 'i', 't', 'z', ' ', 'C', 'a', 'r', 'l', 't', 'o', 'n', ' ', 'a', 't', ' ', '9', ' ', 'o', "'", 'c', 'l', 'o', 'c', 'k', ',', ' ', 'd', 'o', 'n', "'", 't', ' ', 'b', 'e', ' ', 'l', 'a', 't', 'e', '!']

Case 2.2

If you want to list each of the characters from the string given but want to store the numeric value as an integer within the list then:

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
res = [ (int if x.isnumeric() else str) (x) for x in msg ] 
print(res)

Output (notice the number is stored as a int here, not as a string like the previous one):

['M', 'e', 'e', 't', ' ', 'm', 'e', ' ', 'a', 't', ' ', 't', 'h', 'e', ' ', 'R', 'i', 't', 'z', ' ', 'C', 'a', 'r', 'l', 't', 'o', 'n', ' ', 'a', 't', ' ', 9, ' ', 'o', "'", 'c', 'l', 'o', 'c', 'k', ',', ' ', 'd', 'o', 'n', "'", 't', ' ', 'b', 'e', ' ', 'l', 'a', 't', 'e', '!']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Paulius
Solution 4
Solution 5 Chris Doyle
Solution 6 ophact
Solution 7 alphaBetaGamma
Solution 8