Skip to main content
added 450 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

If all you want is to get the file's name and use it to get the right target directory, you can do:

#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the file name
    str1="${i##*/}"

    ## Get the target directory
    dir="/home/userA/folder2/${str1%.txt}/"
done

This is using the shell's native string manipulation features. ${var##pattern} removes the longest match of pattern from the start of $var and ${var%pattern} removes the shortest match of pattern from the end of $var. So, ${i##*/} removes everything until the last / (the path) from the file name and ${i%.txt} removes the string .txt from the end of it.

If all you want is to get the file's name and use it to get the right target directory, you can do:

#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the file name
    str1="${i##*/}"

    ## Get the target directory
    dir="/home/userA/folder2/${str1%.txt}/"
done

If all you want is to get the file's name and use it to get the right target directory, you can do:

#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the file name
    str1="${i##*/}"

    ## Get the target directory
    dir="/home/userA/folder2/${str1%.txt}/"
done

This is using the shell's native string manipulation features. ${var##pattern} removes the longest match of pattern from the start of $var and ${var%pattern} removes the shortest match of pattern from the end of $var. So, ${i##*/} removes everything until the last / (the path) from the file name and ${i%.txt} removes the string .txt from the end of it.

added 36 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

If all you want is to get the file's name and use it to get the right target directory, you can do:

#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the file name
    str1="${i##*/}"

    ## Get the target directory
    dir="/home/userA/folder2/$str1/"
done

In fact, you don't even need the two steps, I only wrote it that way for clarity. You could also simply do:

#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the target directory
    dir="/home/userA/folder2/${i##*/}/"
done
#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the file name
    str1="${i##*/}"

    ## Get the target directory
    dir="/home/userA/folder2/${str1%.txt}/"
done

If all you want is to get the file's name and use it to get the right target directory, you can do:

#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the file name
    str1="${i##*/}"

    ## Get the target directory
    dir="/home/userA/folder2/$str1/"
done

In fact, you don't even need the two steps, I only wrote it that way for clarity. You could also simply do:

#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the target directory
    dir="/home/userA/folder2/${i##*/}/"
done

If all you want is to get the file's name and use it to get the right target directory, you can do:

#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the file name
    str1="${i##*/}"

    ## Get the target directory
    dir="/home/userA/folder2/${str1%.txt}/"
done
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

If all you want is to get the file's name and use it to get the right target directory, you can do:

#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the file name
    str1="${i##*/}"

    ## Get the target directory
    dir="/home/userA/folder2/$str1/"
done

In fact, you don't even need the two steps, I only wrote it that way for clarity. You could also simply do:

#!/bin/bash
for i in /home/userA/folder/*.txt
do
    ## Get the target directory
    dir="/home/userA/folder2/${i##*/}/"
done