As part of my Criminal Analysis project, I have been initially coding everything in R. I am going to eventually convert everything to Python then probably to Julia. In preparation for this, I created my project directory to reflect this (as seen below). The following operations in the command line interface (CLI) will be in Linux/bash, which should provide a practical application of bash commands to conduct administrative tasks.
Make Directories
mkdir
is the bash command to create a directory. If you don’t have all the directories made, you can use the following bash commands to make those directories.
mkdir scripts_python scripts_julia
Alternatively if you have a directory called “scripts” then have a folder for each language inside that, you can use this:
mkdir -p ./scripts/{python,julia}
Copy Files
Now that the desired directories have been created, I demonstrate copying all my current R scripts to my Python directory. First, I’ll show copying a single file. If you have specific files you want to convert this is the best option, calling it explicitly. Check the results of the operation with the ls
bash command on the desired directory.
cp ./scripts_r/project_functions.R ./scripts_python/
# Check copy operation
ls ./scripts_python/
If you are trying to copy all files with a specific pattern into a new location, you can benefit using the following bash command. Here, I’m copying all my scripts in my “scripts_r” direct with the .R
file extension into my “scripts_python” directory.
cp ./scripts_r/*.R ./scripts_python/
# Check copy operation
ls ./scripts_python -l
The ls -l
bash command just lists the files in the desired directory in a long list format.
Using just ls
will produces the following view in the CLI.
If I just wanted to copy over specific files following a pattern, I can do the following to copy all file beginning “dc_” and ending in “.R”. Without getting too much into regex operations, I’ll keep it simple for this post.
cp ./scripts_r/dc_*.R ./scripts_python/
Renaming Files
Now that the desired files have been copied into my desired directory, I can rename all my “.R” files to “.py” files.
cd ./scripts_python/
for file in *.R; do
mv "$file" "$(basename "$file" .R).py"
done
For Loop
This bash command has several things going on. First, I apply a for
loop. I want to loop through each file in the current directory matching the “.R” pattern.
If you are not located in the directory or want to match on a specific pattern, you can use the following command. I will just echo the results to validate it works.
for file in ./scripts_python/dc_*.R; do
echo "$file"
done
Move/Rename Command
Now can rename the files in place using the mv
bash command. The mv
bash command is simply rename or move operation. If the source file and the destination file are located in the same directory, then mv
fill rename the file. If the destination is in a different directory, it will move the file(s).
In my case, I renamed the files in place. With the “$file” variable value, which represents the element in my for loop, I can pull out the base of the filename using the basename
bash command on the “$file” variable with the “.R” file extension. This will return the file name base and combine it with “.py”. Now the files I looped through are converted from an admin standpoint.
# mv SOURCE DESTINATION
mv "$file" "$(basename "$file" .R).py"
Can You Perform the Operation with cp
?
The answer is absolutely. Using the same workflow, you change out the mv
function for the cp
function and skip the original copy task because the two would have been combined.
for file in ./scripts_r/*.R; do
cp "$file" ./scripts_python/"$(basename "$file" .R).py"
done
What’s Next?
In the next follow up post, I will begin demonstrating converting the contents of my R scripts to Python so they function appropriately in the Python language interpreter.
If you want to learn more about the additional options for each bash command, append --help
to each on the CLI. Or you can reference the manual page for the function using the man
in front of the bash command.