Cut file by name in bash [closed]
I have a folder with thousand of images, and I need to cut all the images with a 'F' as a third letter in the filename in another folder. Is there a way to write a bash code to do that? Or how can I do?
linux bash shell folder cut
closed as too broad by F. Hauri, l'L'l, chepner, miken32, jww Nov 16 '18 at 1:15
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have a folder with thousand of images, and I need to cut all the images with a 'F' as a third letter in the filename in another folder. Is there a way to write a bash code to do that? Or how can I do?
linux bash shell folder cut
closed as too broad by F. Hauri, l'L'l, chepner, miken32, jww Nov 16 '18 at 1:15
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have a folder with thousand of images, and I need to cut all the images with a 'F' as a third letter in the filename in another folder. Is there a way to write a bash code to do that? Or how can I do?
linux bash shell folder cut
I have a folder with thousand of images, and I need to cut all the images with a 'F' as a third letter in the filename in another folder. Is there a way to write a bash code to do that? Or how can I do?
linux bash shell folder cut
linux bash shell folder cut
asked Nov 15 '18 at 16:00
user254087user254087
274
274
closed as too broad by F. Hauri, l'L'l, chepner, miken32, jww Nov 16 '18 at 1:15
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by F. Hauri, l'L'l, chepner, miken32, jww Nov 16 '18 at 1:15
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Isn't that simply a matter of using globs:
mv source_folder/??F* destination_folder
man bash:
Pattern Matching
Any character that appears in a pattern, other than the special pattern characters described below, matches itself. The NUL character may not occur in a pattern. A backslash escapes the following character; the escaping backslash is discarded when matching. The special pattern characters must be quoted if they are to be matched literally.
The special pattern characters have the following meanings:
*
Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories.
?
Matches any single character.
add a comment |
If there are enough of them the command might overwhelm the interpreter, especially on older systems. I've had old ksh shells puke when a command went over 1kb. You might need to use a more complex construction.
find source_folder -name '??F*' -maxdepth 1 |
xargs -IF mv F destination_folder
Alternate syntax, probably a lot more efficient -
find source_folder -name '??F*' -maxdepth 1 |
xargs mv -t destination_folder
But if possible for you, simple is better. Try Sam's solution first.
Good luck.
1
Good point. btw: Themv -t
syntax comes in handy with xargs:xargs mv -t destination_folder
– Samuel Kirschner
Nov 15 '18 at 16:30
Excellent point!
– Paul Hodges
Nov 15 '18 at 16:39
add a comment |
Create a .sh file (for example, script.sh) and put the code:
#!/bin/bash
letter_order=3 # Third
search_letter="F" # Find the letter F
for file in *; do
if [ "${file:$letter_order-1:1}" == "$search_letter" ]; then
mv ${file} myNewFolderName/
printf '%sn' "${file}"
fi
done
So, just run the script '$ ./script.sh'
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Isn't that simply a matter of using globs:
mv source_folder/??F* destination_folder
man bash:
Pattern Matching
Any character that appears in a pattern, other than the special pattern characters described below, matches itself. The NUL character may not occur in a pattern. A backslash escapes the following character; the escaping backslash is discarded when matching. The special pattern characters must be quoted if they are to be matched literally.
The special pattern characters have the following meanings:
*
Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories.
?
Matches any single character.
add a comment |
Isn't that simply a matter of using globs:
mv source_folder/??F* destination_folder
man bash:
Pattern Matching
Any character that appears in a pattern, other than the special pattern characters described below, matches itself. The NUL character may not occur in a pattern. A backslash escapes the following character; the escaping backslash is discarded when matching. The special pattern characters must be quoted if they are to be matched literally.
The special pattern characters have the following meanings:
*
Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories.
?
Matches any single character.
add a comment |
Isn't that simply a matter of using globs:
mv source_folder/??F* destination_folder
man bash:
Pattern Matching
Any character that appears in a pattern, other than the special pattern characters described below, matches itself. The NUL character may not occur in a pattern. A backslash escapes the following character; the escaping backslash is discarded when matching. The special pattern characters must be quoted if they are to be matched literally.
The special pattern characters have the following meanings:
*
Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories.
?
Matches any single character.
Isn't that simply a matter of using globs:
mv source_folder/??F* destination_folder
man bash:
Pattern Matching
Any character that appears in a pattern, other than the special pattern characters described below, matches itself. The NUL character may not occur in a pattern. A backslash escapes the following character; the escaping backslash is discarded when matching. The special pattern characters must be quoted if they are to be matched literally.
The special pattern characters have the following meanings:
*
Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories.
?
Matches any single character.
answered Nov 15 '18 at 16:07
Samuel KirschnerSamuel Kirschner
9351717
9351717
add a comment |
add a comment |
If there are enough of them the command might overwhelm the interpreter, especially on older systems. I've had old ksh shells puke when a command went over 1kb. You might need to use a more complex construction.
find source_folder -name '??F*' -maxdepth 1 |
xargs -IF mv F destination_folder
Alternate syntax, probably a lot more efficient -
find source_folder -name '??F*' -maxdepth 1 |
xargs mv -t destination_folder
But if possible for you, simple is better. Try Sam's solution first.
Good luck.
1
Good point. btw: Themv -t
syntax comes in handy with xargs:xargs mv -t destination_folder
– Samuel Kirschner
Nov 15 '18 at 16:30
Excellent point!
– Paul Hodges
Nov 15 '18 at 16:39
add a comment |
If there are enough of them the command might overwhelm the interpreter, especially on older systems. I've had old ksh shells puke when a command went over 1kb. You might need to use a more complex construction.
find source_folder -name '??F*' -maxdepth 1 |
xargs -IF mv F destination_folder
Alternate syntax, probably a lot more efficient -
find source_folder -name '??F*' -maxdepth 1 |
xargs mv -t destination_folder
But if possible for you, simple is better. Try Sam's solution first.
Good luck.
1
Good point. btw: Themv -t
syntax comes in handy with xargs:xargs mv -t destination_folder
– Samuel Kirschner
Nov 15 '18 at 16:30
Excellent point!
– Paul Hodges
Nov 15 '18 at 16:39
add a comment |
If there are enough of them the command might overwhelm the interpreter, especially on older systems. I've had old ksh shells puke when a command went over 1kb. You might need to use a more complex construction.
find source_folder -name '??F*' -maxdepth 1 |
xargs -IF mv F destination_folder
Alternate syntax, probably a lot more efficient -
find source_folder -name '??F*' -maxdepth 1 |
xargs mv -t destination_folder
But if possible for you, simple is better. Try Sam's solution first.
Good luck.
If there are enough of them the command might overwhelm the interpreter, especially on older systems. I've had old ksh shells puke when a command went over 1kb. You might need to use a more complex construction.
find source_folder -name '??F*' -maxdepth 1 |
xargs -IF mv F destination_folder
Alternate syntax, probably a lot more efficient -
find source_folder -name '??F*' -maxdepth 1 |
xargs mv -t destination_folder
But if possible for you, simple is better. Try Sam's solution first.
Good luck.
edited Nov 15 '18 at 16:47
answered Nov 15 '18 at 16:25
Paul HodgesPaul Hodges
3,5151423
3,5151423
1
Good point. btw: Themv -t
syntax comes in handy with xargs:xargs mv -t destination_folder
– Samuel Kirschner
Nov 15 '18 at 16:30
Excellent point!
– Paul Hodges
Nov 15 '18 at 16:39
add a comment |
1
Good point. btw: Themv -t
syntax comes in handy with xargs:xargs mv -t destination_folder
– Samuel Kirschner
Nov 15 '18 at 16:30
Excellent point!
– Paul Hodges
Nov 15 '18 at 16:39
1
1
Good point. btw: The
mv -t
syntax comes in handy with xargs: xargs mv -t destination_folder
– Samuel Kirschner
Nov 15 '18 at 16:30
Good point. btw: The
mv -t
syntax comes in handy with xargs: xargs mv -t destination_folder
– Samuel Kirschner
Nov 15 '18 at 16:30
Excellent point!
– Paul Hodges
Nov 15 '18 at 16:39
Excellent point!
– Paul Hodges
Nov 15 '18 at 16:39
add a comment |
Create a .sh file (for example, script.sh) and put the code:
#!/bin/bash
letter_order=3 # Third
search_letter="F" # Find the letter F
for file in *; do
if [ "${file:$letter_order-1:1}" == "$search_letter" ]; then
mv ${file} myNewFolderName/
printf '%sn' "${file}"
fi
done
So, just run the script '$ ./script.sh'
add a comment |
Create a .sh file (for example, script.sh) and put the code:
#!/bin/bash
letter_order=3 # Third
search_letter="F" # Find the letter F
for file in *; do
if [ "${file:$letter_order-1:1}" == "$search_letter" ]; then
mv ${file} myNewFolderName/
printf '%sn' "${file}"
fi
done
So, just run the script '$ ./script.sh'
add a comment |
Create a .sh file (for example, script.sh) and put the code:
#!/bin/bash
letter_order=3 # Third
search_letter="F" # Find the letter F
for file in *; do
if [ "${file:$letter_order-1:1}" == "$search_letter" ]; then
mv ${file} myNewFolderName/
printf '%sn' "${file}"
fi
done
So, just run the script '$ ./script.sh'
Create a .sh file (for example, script.sh) and put the code:
#!/bin/bash
letter_order=3 # Third
search_letter="F" # Find the letter F
for file in *; do
if [ "${file:$letter_order-1:1}" == "$search_letter" ]; then
mv ${file} myNewFolderName/
printf '%sn' "${file}"
fi
done
So, just run the script '$ ./script.sh'
answered Nov 15 '18 at 16:20
Renan PortoRenan Porto
318
318
add a comment |
add a comment |