FILE MANAGEMENT

folder

Manage and manipulate files.

RENAMING

renaming

The Following commands can be used to rename or manipulate a file or multiple files. Characters can be added, removed or replaced. A script can also be used to automate this process.

01

Replace spaces with underscores

The following commands can be used to replace spaces with underscores on a given filen name or files. If the rename package is not installed on your system by default, it will most likely be in your distributions repositories.

Install rename
Use the following commands to install rename on your system.

fossworkx@linux-server:~$ sudo apt update
fossworkx@linux-server:~$ sudo apt install rename

Replace spaces with underscores (single file)
Use the following command to replace all occurances of spaces with underscores on a given file

fossworkx@linux-server:~$ rename 's/ /_/g' '0 replace my spaces with underscores.txt'

Replace spaces with underscores (multiple files)
Similar to the command above, this one will replace all spaces with underscores; with the execption of renaming all of the files in the current working directory.

fossworkx@linux-server:~$ rename 's/ /_/g' *

Create a script
If you would like to make this process a bit easier, a script can be created. In the script below called SPACEGO.SH, the user will be asked to provide a path to the file they would like to edit. Afer doing so, all spaces will be replaced with underscores in each file name located in the directory given. A listing of the files in the directory will also be shown before and after the files have been altered, with an option to cancel.

#!/usr/bin/bash

echo ""
echo "SPACEGO.SH"
echo ""

echo -n "Target Diarectory: "
read path

eval cd $path

echo ""
echo "Directory Contents:"
ls -l
echo ""

echo "Replace all spaces with underscores?"
read -p "Hit <ENTER> to continue / ctrl + c to quit"

rename 's/ /_/g' *

echo ""
echo "Success! generating results..."
sleep 2
echo ""
ls -l

exit

02

Replace underscores with spaces

The following commands can be used to replace spaces with underscores on a given file name or files. If the rename package is not installed on your system by default, it will most likely be in your distributions repositories.

Install rename
Use the following commands to install rename on your system.

fossworkx@linux-server:~$ sudo apt update
fossworkx@linux-server:~$ sudo apt install rename

Replace underscores with spaces (single file)
Use the following command to replace all occurances of underscores with spaces on a given file

fossworkx@linux-server:~$ rename 's/_/ /g' '0_replace_my_underscores_with_spaces.txt'

Replace underscores with spaces (multiple files)
Similar to the command above, this one will replace all underscores with spaces; with the execption of renaming all of the files in the current working directory.

fossworkx@linux-server:~$ rename 's/_/ /g' *

03

Replace or remove a pattern

The following commands can be used to replace or remove a pattern on a given file name or files. If the mmv package is not installed on your system by default, it will most likely be in your distributions repositories.

Install mmv
Use the following commands to install mmv on your system.

fossworkx@linux-server:~$ sudo apt update
fossworkx@linux-server:~$ sudo apt install mmv

Replace or remove a pattern
Use the following command to replace a pattern of text in all files located in the current working directory. This command will replace all instances of foo with the word bar

fossworkx@linux-server:~$ mmv '*foo*' '#1bar#2'

Create a script
If you would like to make this process a bit easier, a script can be created. In the script below called RENAME.SH, the user will be asked to provide a path to the file or files they would like to edit. Afer doing so, they will be asked to provide the pattern in the file name they would like to change, then what they would like to change it too. A listing of the files in the directory will also be shown before and after the files have been altered, with an option to cancel.

#!/usr/bin/bash

echo ""
echo "RENAME.SH"
echo ""

echo -n "Target Diarectory: "
read path

eval cd $path

echo ""
echo "Directory Contents:"
ls -l
echo ""

echo -n "Start Pattern: "
read pattern_start

echo -n "End Pattern: "
read pattern_end

echo ""
echo "\"$pattern_start\" will be replaced with \"$pattern_end\""
read -p "Hit <ENTER> to continue / ctrl + c to quit"

mmv "*$pattern_start*" "#1$pattern_end#2"

echo ""
echo "Success! generating results..."
sleep 2
echo ""
ls -l

exit