\chapter{Files} \section{Remove Part of a Filename from Multiple Files} \lettrine{S}{ome} applications can produce files that are similarly named, and for whatever reason, you want to remove the repetitive bits. You can use the \texttt{mmv}\index{mmv} package to do this. Assume you have a list of files that all start with ``foo'': \begin{Verbatim}[] foo143.txt fooabi.txt foo38r.txt \end{Verbatim} To remove the ``foo'', use this command: \begin{Verbatim}[frame=single, breaklines=true] mmv 'foo*' '#1' \end{Verbatim} To change ``foo'' to ``bar'', use: \begin{Verbatim}[frame=single, breaklines=true] mmv 'foo*' 'bar#1' \end{Verbatim} The \texttt{mmv} command uses this command structure: \vspace{3mm} \begin{Verbatim}[frame=single, breaklines=true, framesep=3mm, label=\fbox{mmv Command Structure}] mmv [-m|x|r|c|o|a|l|s] [-h] [-d|p] [-g|t] [-v|n] [--] [from to] \end{Verbatim} For a full discussion of those, see the ``man'' page. The key concept here is that wildcard characters in the ``from'' option get replaced with ``\#1'', ``\#2'' etc in the ``to'' option, in the order they are encountered. So it is possible to reorder elements of file names. For example, to exchange ``foo'' in our list above for the file extension, we would use: \begin{Verbatim}[frame=single, breaklines=true] mmv 'foo*.txt' 'txt#1.foo' \end{Verbatim} To reverse the order of the last three characters of the filenames, we would use: \begin{Verbatim}[frame=single, breaklines=true] mmv 'foo???.txt' 'foo#3#2#1.txt' \end{Verbatim} That would produce this list of files:\index{mmv} \begin{Verbatim}[] foo341.txt fooiba.txt foor83.txt \end{Verbatim} \lettrine{T}{his} can also be accomplished by using the \textbf{rename}\index{rename} command. \section{Obtain a List of Files and their Paths} \lettrine{T}{he} \texttt{find} command has options to obtain this list. To obtain this list with the full path, use: \input{bits/find_PWD} To obtain this list with a relative path, use: \begin{Verbatim}[frame=single, breaklines=true] find . \end{Verbatim} To find only certain types of files (in this case jpgs) and redirect this information to a file, use something like this: \begin{Verbatim}[frame=single, breaklines=true] find . \( name "*.jpg" -o name "*.jpeg" \) > jpglist.txt \end{Verbatim} Because this is bash, the parentheses are not a capture group (as in regex), but operators used to force precedence and need to be escaped, because parentheses are special to the shell. (Normally we would use quotation marks, but because we need to use quotation marks with the \texttt{name} test, we are using backslashes). Likewise, the \texttt{-o} is a Boolean ``or'', meaning that the second expression will not be evaluated if the first one is true. For more information, use \texttt{man find}. \ktcbox{Sometimes you need a list of files with their filepaths, and these tools will do that. Unfortunately, Linux does not have a way to put the file first, so the way to do this is to redirect \texttt{stdout} to a \texttt{.csv} file and manipulate the output in a spreadsheet.}