man ls
53 Introduction to the System Shell
This is a very brief introduction to some of the most commonly used shell commands.
A shell is a command line interface allowing access to an operating system’s services. Multiple different shells exist. The most popular is probably bash, which is the default in most Linux installations. In macOS, the default shell switched form bash to zsh in 2019 with the release of Catalina. In Windows, various shells are available through the Windows Subsystem for Linux.
The commands listed here will work similarly in all/most shells.
53.1 Common shell commands
The first thing to look for in a new environment is the help system. In the shell, this is accessed with man
:
53.1.1 man
: Print the manual pages
For example, the following would return the manual pages for the ls
command, explained a little later.
53.1.2 pwd
: Print working directory (the directory you are currently in)
pwd
53.1.3 cd
: Set working directory to /path/to/dir
cd /path/to/dir
53.1.4 ls
: List directory contents
ls
adding the -l
argument, prints directory contents as a list
ls -l
53.1.5 mv
: Move file
from /current/dir/
to /new/dir
mv /current/dir/file /new/dir
53.1.6 mv
: Rename file
to newfilename
mv /current/dir/file /current/dir/newfilename
53.1.7 cp
: Make a copy of file
from currentPath
into altPath
cp /currentPath/file /altPath/file
53.1.8 rm
: Remove, i.e. delete, file
rm /path/to/file
53.1.9 mkdir
: Create a new directory named ‘newdir’
mkdir /path/to/newdir
53.1.10 rmdir
: Remove, i.e. delete, empty directory
rmdir /path/to/somedir
To remove a non-empty directory and all of its contents, you can use rm -rf
: -r
is recursive; -f
is force.
Note: Use with care! it will immediately delete all content in the directory without asking for confirmation
rm -rf /path/to/dir
53.1.11 cat
: Print contents of file
to the console
cat /path/to/file
53.1.12 uname
: Get system information
-a
argument for “all”
uname -a
53.1.13 whoami
: Print the currently logged in user’s name
whoami
53.1.14 id
: Return user identity
The id
command returns, among other things, the groups a user belong to. This informs you which directories and files a user can read, write, and execute.
id username
53.2 Running system commands within R
You can execute any system command within R using the system()
command:
system("uname -a")
53.3 Useful terminal commands for working with data
If you receive a data file, you may want to get an idea of the contents before reading it into R.
53.3.1 head
: Print the first few lines of a file
head iris.csv
Sepal.Length,Sepal.Width,Petal.Length,Petal.Width,Species
5.1,3.5,1.4,0.2,setosa
4.9,3,1.4,0.2,setosa
4.7,3.2,1.3,0.2,setosa
4.6,3.1,1.5,0.2,setosa
5,3.6,1.4,0.2,setosa
5.4,3.9,1.7,0.4,setosa
4.6,3.4,1.4,0.3,setosa
5,3.4,1.5,0.2,setosa
4.4,2.9,1.4,0.2,setosa
You can print the first n
lines using the syntax head -n /path/to/file
.
For example, you can print just the first line, which would hold the column names in a CSV file:
head -1 iris.csv
Sepal.Length,Sepal.Width,Petal.Length,Petal.Width,Species
53.3.2 tail
: Print the last few lines of a file
tail iris.csv
6.7,3.1,5.6,2.4,virginica
6.9,3.1,5.1,2.3,virginica
5.8,2.7,5.1,1.9,virginica
6.8,3.2,5.9,2.3,virginica
6.7,3.3,5.7,2.5,virginica
6.7,3,5.2,2.3,virginica
6.3,2.5,5,1.9,virginica
6.5,3,5.2,2,virginica
6.2,3.4,5.4,2.3,virginica
5.9,3,5.1,1.8,virginica
53.3.3 wc
: Word, line, character, and byte count
wc
can print the word or line count of a file, among other things. This can be particularly useful when dealing with large files
wc -l iris.csv
151 iris.csv
The iris dataset consist of 150 cases plus one line with the column names
53.3.4 du
: Display disk usage statistics
du
can display the size of a file or directory among other things.
This can be very important when trying to determine if the contents of a data file will fit in memory.
Display the size of a file in human-readable format (Kilobytes, Megabytes, Gigabytes, etc)
du -sh iris.csv
4.0K iris.csv
The same command can be used on an entire current directory:
du -sh .
874M .
53.4 Further Resources
Bash is the default shell in most Linux distributions.
ZSH replaced Bash as the default shell in macOS with the release of macOS Catalina in 2019 (though Bash currently still comes installed)