Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Python Equivalent of Shell Commands

Shell CommandAlternativePython
which gitshutil.which("git")shutil
mkdir -p /path/to/filePath("path/to/some/file").mkdir(exist_ok=True)pathlib
os.makedirs("/path/to/file", exist_ok=True)os
!mkdir -p /path/to/fileIPython
mkdir -p /path/to/filexonsh
cp file1 file2shutil.copy2("file1", "file2")shutil
shutil.copyfile("file1", "file2")shutil
!cp file1 file2IPython
cp file1 file2xonsh
cp -ir dir1/* dir2shutil.copytree("dir1", "dir2")shutil
cp -r dir1/* dir2shutil.copytree("dir1", "dir2", dirs_exist_ok=True)shutil
cp -ir /path/to/dir1/ dir2/shutil.copytree("/path/to/dir1/", "dir2/dir1")shutil
ln -s file1 file2Path("file1").symlink_to("file2", target_is_directory=True)pathlib
os.symlink("file1", "file2", target_is_directory=True)os
!ln -s file1 file2IPython
ln -s file1 file2xonsh
ln -sT file1 file2Path("file1").symlink_to("file2", target_is_directory=False)pathlib
os.symlink("file1", "file2", target_is_directory=False)os
!ln -sT file1 file2IPython
ln -sT file1 file2xonsh
rm filePath("/path/to/file").unlink()pathlib
os.remove(file)os
!rm fileIPython
rm filexonsh
rmdir /path/to/dirPath("/path/to/dir").rmdir()pathlib
rm -rf dirshutil.rmtree(dir)shutil
!rm -rf dirIPython
rm -rf dirxonsh
rm -rf file_or_dirif p.is_file():
        p.unlink()
else:
        shutil.rmtree(p)
pathlib + shutil
!rm -rf file_or_dirIPython
rm -rf file_or_dirxonsh
mv file1 file2Path("file1").rename("file2")pathlib
Path("file1").replace("file2")pathlib
shutil.move("file1", "file2")shutil
os.rename("file1", "file2")os
!mv file1 file2IPython
mv file1 file2xonsh
chmod 600 /path/to/filePath("/path/to/file").chmod(0o600)pathlib

IPython

IPython is the best and simpliest Python approach to replace (all part of) shell so far.

  1. Use the IPython shell or JupyterLab notebook (preferred) instead of Shell for complicated interactive operations.

  2. Be careful about illegal shell commands. For example, ls ) in Bash shell throws the error message bash: syntax error near unexpected token ). If you have a equivalent IPython command, it will throw the same error message. For example, suppose file is path of a file which contains ) then !ls {file} in IPython will throws the same error message as above. However, this is definitely trickier to debug than the original Bash shell command ls ). There are several ways to avoid this. First, you can use Python script (xonsh is a great choice is vanilla Python script is too verbose) instead Shell as underlying commands. Second, you can show the underlying Shell commands for debugging.

  3. You can even run Shell commands on a remote server (via ssh or a remote kernel) in JupyterLab notebook. This provide the advantage of leveraging the JupyterLab notebook UI.

xonsh

xonsh is another great Python approach to replace shell. Unlike IPython, xonsh does not require the prefix ! to run arbitrary shell command. However, there is one flaw of xonsh. You cannot use $() and friends in the middle of an argument, which limits its usability serious for complicated shell commands. Please refer to this issue for more details.

plumbum

Yet another Python approach as a replacement of shell. I personally prefer IPython and xonsh to plumbum.

References