How Do I Linux?
A brief intro to some basic Linux commands
One of the great things about being a developer is that we get to see projects using multiple lenses. We undoubtedly will end up preferring some language or framework or library…but we will have the opportunity to explore a number of different environments. And that keeps things interesting, right?
Absolutely! Except when it’s kind of stressful because you have some important project to complete and you have to scramble to learn something new by an impending deadline. So I think it’s good practice to make sure you’re up to speed on some basic principles that you are likely to encounter during an average programmer’s experience, whether for a coding interview or on the job.
Linux
Linux commands are one of those basic principles. You may be asked during a coding challenge, for example, to spin up a virtual machine using Linux, or for scripting on the job.
Perhaps the most important command and the one that most people struggle with is
sudo
What is sudo
? Certain Linux applications will require special privileges; similar to when a Windows or MacOS application shows a prompt confirming you want to continue with a particular action (and may ask you to enter an admin password), a Linux application will outright deny an action that requires administrative privileges if the user does NOT have those privileges.
The sudo
command, then, acts like a barrier between those administrative tasks and normal ones, meaning that sudo
will allow you to confirm execution of the action as well as confirm that you have the proper credentials to execute that action.
For example, a non-admin user may try to enter a command in Linux to install a piece of software. But this will throw an error because a non-admin user cannot install software.
In order to bypass this error, you can place sudo
in front of the command, and..voila’, the command can be executed.
You can also use the su
command to switch to the superuser (root). If you’re not sure whether to use sudo
or su
, look at the trailing character on the command line. If it’s a pound sign (#), you’re logged in as root.
If you want to edit a file directly in the administrative directory, you can use the command sudo nano <file path>
. This allows for editing privileges and execution privileges.
It can be very helpful to be able to check a file’s permissions and Linux has the ability to do just that! You can use the ls-l
command and will see most likely see some combination of rwx
in front of the file information. The letters stand for: r
for read, w
for write and x
for execute, which represent the permissions given to those files. If there is a d
in front of the rwx
, for example (for example, -drwx
) this means directory. To add or remove permissions to the user, use the command chmod
with a +
or –
, along with the r
(read), w
(write), x
(execute) attribute followed by the name of the directory or file. For example:
chmod +rwx <filename>
chmod –rwx <directory name>
A Few Other Useful Commands
Besides the sudo
command, there a few others that you should keep in your wheelhouse.
ls
This is the “list” command, which displays all of the major directories filed under a given file system. For example,
ls/myapplications
will display all the files and folder in the myapplications
folder.
2. cd
This is the “change directory” command, which allows users to change between file directories. For example, if you wanted to change from the home directory to the fluffer
directory, you would use
cd/fluffer/myapplications
which means “change to the fluffer directory which is the myapplications directory.” All Linux commands follow this logical path (“change to the x directory, which is stored in the y directory”).
3. mv
This is the “move” command, which, as its name implies, allows a user to move a file to another folder or directory. For example, you could write
mv/fluffer/myapplications/greatideas /fluffer/myapplications/shelved
The first part of the command mv/fluffer/myapplications/greatideas
lists the application to be moved (fluffer
, in this case). The second part of the command /fluffer/myapplications/shelved
lists where fluffer
will be moved to, from greatideas
to shelved
.
4. mkdir
This is the “make directory” command, which allows a user to make a new directory. To make a new directory named “new directory”, you simply type
mdkir newdirectory
Keep in mind that mkdir
will make an empty directory, it does NOT make files.
5. rmdir
This is the “remove directory” command, which allows a user to remove an existing directory. You could remove the “new directory” directory by typing
rmdir newdirectory
Keep in mind that rmdir
will remove an empty directory, it does NOT remove a directory that contains files.
6. touch
This is essentially the “make a file” command, which allows users to make an empty file. An example would be
touch newfile.txt
which would create an empty text file.
7. rm
This is the “remove” command, which deletes created files. For example
rm newfile.txt
will remove the file labeled newfile
. An interesting thing to note: the rm
command will actually remove BOTH files and directories…and the directories may have files in them! This means that the rm
command is more “powerful” than the rmdir
command so should be used with care.
8. locate
This command is essentially a “find” command. This command is very convenient when you aren’t sure of the name of a file or where it is saved. Here is an example
locate -i *travel*trip**share*
which will locate any and all files whose name contains “Red”, “House”, and “City”, thanks to the inclusion of the asterisk (*
), which is considered a “wildcard” search. Using -i
means “search for a file regardless of capitalization.”
These is obviously NOT an exhaustive list of all Linux commands, but gives a good idea of the most commonly used ones.