Skip to content

Common Unix Command Line Operations and Their Python Equivalents

Learning by analogy.

Unix Command Line Operation Python Operation
pwd https://docs.python.org/2/library/os.html#os.getcwd
cd https://docs.python.org/2/library/os.html#os.chdir
mkdir https://docs.python.org/2/library/os.html#os.mkdir
mkdir -p https://docs.python.org/2/library/os.html#os.makedirs
rm https://docs.python.org/2/library/os.html#os.remove
rmdir https://docs.python.org/2/library/os.html#os.rmdir
rm -r https://docs.python.org/2/library/shutil.html#shutil.rmtree
mv https://docs.python.org/2/library/os.html#os.rename
ln https://docs.python.org/2/library/os.html#os.link
ln -s https://docs.python.org/2/library/os.html#os.symlink
echo $VAR https://docs.python.org/2/library/os.html#os.getenv
export VAR=foo https://docs.python.org/2/library/os.html#os.putenv
ls https://docs.python.org/2/library/subprocess.html#subprocess.check_output
ls -l https://docs.python.org/2/library/subprocess.html#subprocess.check_output
chmod https://docs.python.org/2/library/os.html#os.chmod
cp https://docs.python.org/2/library/shutil.html#shutil.copy
cp -p https://docs.python.org/2/library/shutil.html#shutil.copy2
cp -r https://docs.python.org/2/library/shutil.html#shutil.copytree
date datetime
ps, top psutil

Generally useful: shlex.split