User Tools

Site Tools


howto:linux_basics

Intro to using Linux via a command-line interface

The CLI, or command-line interface, is a very powerful way to interact with any computer. In Linux especially, the CLI is often the primary way to perform tasks. Additionally, you can write scripts to automate tasks over the command-line. So even when there is a GUI option, if you want to automate a task, learning how to do it using the command-line is essential.

A terminal is the actual application that you use (i.e. Terminal.app, cmd.exe, PowerShell, PuTTY, etc); it is the command-line environment on your computer from which you can use SSH to access the remote linux server.

SSH

SSH stands for “Secure SHell” and it is both a tool to open a connection to a remote server and the protocol that is used for the connection. A Shell is the actual command-line interface that you use; it is an interactive program where you can run commands to do things like navigate the file system ande run other programs (i.e. stata). I'll review basic commands below, and provide a link to further self-paced training.

The SSH protocol is Secure because it encrypts the communication channel. No one listening on the network can see any data or information about the connection other than where you are coming from and where you are connecting. Using SSH to encrypt communication is the backbone of many data security agreements.

Using SSH on Windows

Windows does not have a built-in SSH client application. You will need to install a command-line environment, such as the Windows Subsystem for Linux, or an SSH connection tool, such as PuTTY.exe

If you use PuTTY, then simply download and run the PuTTY.exe program, then fill in the Host Name (or IP Address) field and click Open.

If you use WSL, then you would open the WSL terminal and type the following command to connect to the linux server:

ssh username@server
  • where username is your user account name on the server
  • where server is the FQDN of the server

Using SSH on Mac

There is already a built-in application you can use on Mac. Simply open /Applications/Utilities/Terminal.app

And then run the command:

ssh username@server
  • where username is your user account name on the server
  • where server is the FQDN of the server

List of Basic Commands

Command Description
passwd
Change your password
ls
ls -l 
List files/folders in the current directory1)
cd myfolder/
cd ../
Change directory to another folder in your current folder or go up one directory to the parent folder
cd /data/my/example/folder/
Change directory to another folder outside your current directory. Notice the leading / indicating an absolute path2)
cd
Change directory back to your home directory
pwd
Print your current working directory
mkdir newname
Make a directory named “newname”
rmdir newname
Remove an empty directory named “newname” (only works if it is empty)
rm myfile
Delete a file “myfile”
rm file*
rm -f file*
Delete all files matching the pattern “file*” - be very careful with this!3)
cp myfile myfile2
Copy a file named “myfile” to a new file named “myfile2”
mv myfile myfile2
Move a file named “myfile” to a new file named “myfile2”
cat somedata.txt
less somedata.txt
Display the contents of a file named “somedata.txt”4)
grep "string" myfiles*
Search for the word “string” in all files with filenames matching the pattern “myfiles*”5), then print matching results
man grep
Read a manual page (help page) about the command “grep” or any command you specify
stata
Run stata, if stata is installed

Tab Completion

You can also use tab-completion by typing a command and hitting the “tab” key twice to show potential commands that match what you have typed. For example, if there are multiple versions of stata installed, type “stata” and hit tab twice to see all matching commands.

Self-paced online lessons

There is a lot more you can do. This brief overview is just an idea to get you started. I highly recommend the links below for additional resources. They have lessons that you can work through on your own to become more familiar with all the above commands and go even more in depth:

1)
-l is an option to show a detailed listing, with information such as file size, permissions, and timestamp
2)
see this link for more information about absolute vs relative paths and how to navigate directories
3)
It is a good idea to test a pattern with ls file* to make sure you are erasing what you intend. There is NO RECYCLE BIN in Linux, but it will ask you to confirm deleting each file. If you are sure you want to erase all matching files, and you don't want to confirm each one by pressing “y”, then you can use the option -f to force the deletion
4)
cat prints to the screen, less is interactive and you use arrow keys or j/k keys to move down/up, respectively
5)
read more about pattern matching here
howto/linux_basics.txt · Last modified: 2024/06/17 21:07 by jnilsson