Skip to main content
added 65 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
for filename in "$1"/*; do
    if [ ! -f "$filename" ] ||&&
       [ "${filename%.old}" != "$filename" ]
    then
        continue
    fi

    mv -- "$filename" "$filename.old"
    fi
done 

Use grep for text, and use the output of ls for reading with your eyes, never as input to a program.

The error that you get is due to a syntax error in your code. The syntax of a for loop (to loop over a number of words) is

for variable in word-list; do ...; done

The code at the top will loop over all regular files in the directory given in $1 and give them an .old filename suffix if they don't already have it. The two tests tests whether $filename refers to a regular file (or a symbolic link to one), and whether $filename, when removing .old from the end of its value, remains the same (i.e. whether it already has the suffix or not).

Another way of doing more or less the same thing is with find:

find "$1" -maxdepth 1 -type f ! -name '*.old' -exec mv {} {}.old ';'

Note that both solutions would overwrite any already existing .old file if there is a corresponding filename without the .old suffix. If there was a directory with an .old suffix under $1, the file would be put into that directory instead of being renamed.

The difference between the find solution and the shell loop is that the find solution would also care about hidden names, and that the shell loop would rename symbolic links to regular files.

Related:

for filename in "$1"/*; do
    if [ ! -f "$filename" ] ||
       [ "${filename%.old}" != "$filename" ]
    then
        continue
    fi

    mv -- "$filename" "$filename.old"
done 

Use grep for text, and use the output of ls for reading with your eyes, never as input to a program.

The error that you get is due to a syntax error in your code. The syntax of a for loop (to loop over a number of words) is

for variable in word-list; do ...; done

The code at the top will loop over all regular files in the directory given in $1 and give them an .old filename suffix if they don't already have it.

Another way of doing more or less the same thing is with find:

find "$1" -maxdepth 1 -type f ! -name '*.old' -exec mv {} {}.old ';'

Note that both solutions would overwrite any already existing .old file if there is a corresponding filename without the .old suffix.

The difference between the find solution and the shell loop is that the find solution would also care about hidden names, and that the shell loop would rename symbolic links to regular files.

Related:

for filename in "$1"/*; do
    if [ -f "$filename" ] &&
       [ "${filename%.old}" = "$filename" ]
    then
        mv -- "$filename" "$filename.old"
    fi
done 

Use grep for text, and use the output of ls for reading with your eyes, never as input to a program.

The error that you get is due to a syntax error in your code. The syntax of a for loop (to loop over a number of words) is

for variable in word-list; do ...; done

The code at the top will loop over all regular files in the directory given in $1 and give them an .old filename suffix if they don't already have it. The two tests tests whether $filename refers to a regular file (or a symbolic link to one), and whether $filename, when removing .old from the end of its value, remains the same (i.e. whether it already has the suffix or not).

Another way of doing more or less the same thing is with find:

find "$1" -maxdepth 1 -type f ! -name '*.old' -exec mv {} {}.old ';'

Note that both solutions would overwrite any already existing .old file if there is a corresponding filename without the .old suffix. If there was a directory with an .old suffix under $1, the file would be put into that directory instead of being renamed.

The difference between the find solution and the shell loop is that the find solution would also care about hidden names, and that the shell loop would rename symbolic links to regular files.

Related:

added 65 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
for filename in "$1"/*; do
    if [ ! -f "$filename" ] ||
       [ "${filename%.old}" != "$filename" ]
    then
        continue
    fi

    mv -- "$filename" "$filename.old"
done 

Use grep for text, and use the the output of ls for reading with your eyes, never as input to a program.

The error that you get is due to a syntax error in your code. The syntax of a for loop (to loop over a number of words) is

for variable in word-list; do ...; done

The code at the top will loop over all regular files in the directory given in $1 and give them an .old filename suffix if they don't already have it.

Another way of doing more or less the same thing is with find:

find "$1" -typemaxdepth f1 -maxdepthtype 1f ! -name '*.old' -exec mv {} {}.old ';'

Note that both solutions would overwrite any already existing .old file if there is a corresponding filename without the .old suffix.

The difference between the find solution and the shell loop is that the find solution would also care about hidden names, and that thothe shell loop would rename symbolic links to regular files.

Related:

for filename in "$1"/*; do
    if [ ! -f "$filename" ] ||
       [ "${filename%.old}" != "$filename" ]
    then
        continue
    fi

    mv -- "$filename" "$filename.old"
done 

Use grep for text, and use the the output of ls for reading with your eyes, never as input to a program.

The error that you get is due to a syntax error in your code. The syntax of a for loop (to loop over a number of words) is

for variable in word-list; do ...; done

The code at the top will loop over all regular files in the directory given in $1 and give them an .old filename suffix if they don't already have it.

Another way of doing more or less the same thing is with find:

find "$1" -type f -maxdepth 1 ! -name '*.old' -exec mv {} {}.old ';'

Note that both solutions would overwrite any already existing .old file if there is a corresponding filename without the .old suffix.

The difference between the find solution and the shell loop is that the find solution would also care about hidden names, and that tho shell loop would rename symbolic links to regular files.

for filename in "$1"/*; do
    if [ ! -f "$filename" ] ||
       [ "${filename%.old}" != "$filename" ]
    then
        continue
    fi

    mv -- "$filename" "$filename.old"
done 

Use grep for text, and use the output of ls for reading with your eyes, never as input to a program.

The error that you get is due to a syntax error in your code. The syntax of a for loop (to loop over a number of words) is

for variable in word-list; do ...; done

The code at the top will loop over all regular files in the directory given in $1 and give them an .old filename suffix if they don't already have it.

Another way of doing more or less the same thing is with find:

find "$1" -maxdepth 1 -type f ! -name '*.old' -exec mv {} {}.old ';'

Note that both solutions would overwrite any already existing .old file if there is a corresponding filename without the .old suffix.

The difference between the find solution and the shell loop is that the find solution would also care about hidden names, and that the shell loop would rename symbolic links to regular files.

Related:

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

for filename in "$1"/*; do
    if [ ! -f "$filename" ] ||
       [ "${filename%.old}" != "$filename" ]
    then
        continue
    fi

    mv -- "$filename" "$filename.old"
done 

Use grep for text, and use the the output of ls for reading with your eyes, never as input to a program.

The error that you get is due to a syntax error in your code. The syntax of a for loop (to loop over a number of words) is

for variable in word-list; do ...; done

The code at the top will loop over all regular files in the directory given in $1 and give them an .old filename suffix if they don't already have it.

Another way of doing more or less the same thing is with find:

find "$1" -type f -maxdepth 1 ! -name '*.old' -exec mv {} {}.old ';'

Note that both solutions would overwrite any already existing .old file if there is a corresponding filename without the .old suffix.

The difference between the find solution and the shell loop is that the find solution would also care about hidden names, and that tho shell loop would rename symbolic links to regular files.