0

I want to move some files in a directory, using their filename length as the criteria.

For example, I want to move any files longer that 10 characters.

I assumed I need an if loop in bash script, but I'm not sure how to proceed.

user1551817
  • 6,693
  • 22
  • 72
  • 109
  • 1
    Use a loop and this http://unix.stackexchange.com/questions/92463/in-bash-how-does-one-determining-the-length-of-filename. Search online for loops in bash. – Suvarna Pattayil Apr 04 '16 at 17:16
  • 1
    I actually don't know how to do all of these things but let's take it one step at a time... You already know some of the functions you'll need; namely finding files in a directory, getting the length of a string, doing a comparison (less than/more than) and moving files. Here's how to [find the length of a string](https://stackoverflow.com/questions/17368067/length-of-string-in-bash), the `ls` command will list the contents of a directory, `mv` is used to move files and `if [ some_condition ]` can be used to compare/test expressions. Try to work with that and search for examples and man pages. – jDo Apr 04 '16 at 17:20

1 Answers1

4

use this template

for f in *; do if [ ${#f} -gt 10 ]; then echo $f; fi; done

replace echo with your mv command.

note that directories will be in the list too.

karakfa
  • 66,216
  • 7
  • 41
  • 56