- Get four Apple AirTags for just $73 with this Black Friday deal
- I tested Beats' new Pill speaker and it delivered gloriously smooth sound (and it's on sale for Black Friday)
- These Sony headphones are a fan favorite - and $150 off for Black Friday
- I tested a 'luxury' nugget ice maker, and it's totally worth it - plus it's $150 off for Black Friday
- The Dyson Airwrap is $120 off ahead of Black Friday - finally
Getting started on the Linux (or Unix) command line — Part 1
To get started as a Linux (or Unix) user, you need to have a good perspective on how Linux works and a handle on some of the most basic commands. This first post in a “getting started” series examines some of the first commands you need to be ready to use.
On logging in
When you first log into a Linux system and open a terminal window or log into a Linux system from another system using a tool like PuTTY, you’ll find yourself sitting in your home directory. Some of the commands you will probably want to try first will include these:
pwd -- shows you where you are in the file system right now (stands for “present working directory”) whoami – confirms the account you just logged into date -- show the current date and time hostname -- display the system’s name
Using the whoami command immediately after logging in might generate a “duh!” response since you just entered your assigned username and password. But, once you find yourself using more than one account, it’s always helpful to know a command that will remind you which you’re using at the moment.
These commands will generate output like this:
$ pwd /home/justme $ whoami justme $ date Tue Nov 21 10:40:20 AM EST 2023$ $ hostname venus
Listing your files
One of the first things you will likely want to do is look at the files and directories that are associated with your account. The command below provides a “long listing” – one that shows all the files and directories in the current file system location along with the permissions and ownership of those files and directories.
$ ls -al total 16 drwx------. 1 justme justme 96 Nov 6 12:21 . drwxr-xr-x. 1 root root 56 Nov 6 12:15 .. -rw-r--r--. 1 justme justme 18 Feb 5 2023 .bash_logout -rw-r--r--. 1 justme justme 141 Feb 5 2023 .bash_profile -rw-r--r--. 1 justme justme 526 Nov 11 12:21 .bashrc drwxr-xr-x. 1 justme justme 34 Apr 13 2023 .mozilla -rw-------. 1 justme justme 1445 Nov 6 12:21 .viminfo -rw-r-----. 1 justme justme 35 Nov 21 12:34 myfile
In the output shown above, there are three directories (lines starting with “d”) and five files lines starting with “-“). The details are only provided because the ls command includes the -a (show all) and -l (provide details) options.
Note that the owner and group are both “justme”. By default, any particular user will be the only member of a same-named user group. Both are set up when a new user is added to the system.
The dates and times that are displayed in long listings show when the files were last updated.
Moving around the file system
To move into any of the directories, use a command like “cd .mozilla”. The current directory, wherever you’re sitting in the file system, can be addressed as “.”. Using a command like “cd .”, therefore, doesn’t accomplish anything. It means “move to where I already am”. Using the “cd ..” command, on the other hand, will move you up one directory – toward /, the base of the file system.
$ pwd /home/justme $ cd .. $ pwd /home $ cd /tmp $ pwd /tmp $ cd $ pwd /home/justme
Commands like “cd /tmp” will take you to the specified directory. A cd command without arguments will always take you back to your home directory as would “cd ~” or a command like “cd /home/justme”. Most Linux users, however, prefer typing as little as needed and just use cd to get back home.
Note that there will always be directories that you will be unable to access unless you switch to the root (superuser) account or use the su (switch user) command to use a different account that has access rights. If you try to move into a directory for which you don’t have adequate access, you’ll see an error like this one:
$ cd /home/newuser -bash: cd: /home/newuser: Permission denied
To see why access was denied, try listing the directory’s permissions like this:
$ ls /home/newuser drwx------. 1 newuser newuser 80 Jul 24 10:48 newuser
The only account that has access rights to this directory is “newuser”.
NOTE: You will likely be set up to use the su command unless your role involves managing the system.
Understanding file and directory permissions
To understand what you can access and not access, it’s important to understand how file and directory permissions work. The first character in the permissions strings like the “d” in “drwxr—–” represents the file type. Most entries in a directory listing will be one of the following.
d = directory - = file
The rest of the permissions string displays the permissions given to the file/directory owner, the owner’s primary group and everyone else. If we break the permissions string into groups, it might look like this:
d rwx r-- --- ^ ^ ^ ^ | | | | | | | +-- everyone else’s permissions (no access) | | +------ other group members’ permissions (read only) | +---------- owner’s permissions (read, write and execute) +------------- file type (d = directory)
The “rwx” shown in the example above means that read (r), write (w) and execute (x) permissions are provided to the directory owner. In contrast, “—” means that none of these permissions are provided. Expect to see many instances in which only read (r–) or read and execute (r-x) permissions are granted. You can control who can access your files. For example, you could grant access to a file in your directory with a command like this provided the intended user has access to your home directory as well.
$ chmod o+r myfile
The command shown above would give read access to anyone in the “other” group (i.e., anyone who is neither the owner nor a member of the file’s assigned group). Keep in mind that these users would not have access if they don’t also have access to the directory containing the file.
NOTE: As you likely suspect, only the owner of a file or root can change the file’s permissions.
Viewing file content
Viewing the content of a file depends on the file type. To view text files, you can use the cat (show file content) or the more (show file content a screenful at a time) command. Bash scripts are text files as are startup files like .bashrc. System executables, on the other hand, which are referred to as “binary” files cannot be viewed except with commands that display the content as a long series of 0’s and 1’s – binary content. Finding a command that can display the content (like an image) on the desktop is an altogether different process.
$ cat myfile This is my favorite file. It reminds me about how I felt when I first Discovered Linux! $ head -5 colors aqua azure black blue bronze $ tail -5 colors turquoise violet wheat white yellow $ more colors aqua azure black blue bronze brown chocolate …
Note that the more command will list file content a screenful at a time. Hit the enter key to move the next screenful.
Copying, moving and deleting files
To make a copy of a file, use the cp (copy) command like this:
$ cp myfile myfile.save
To move a file to a different directory (one you have write access to), use a command like this:
$ mv myscript ~/bin
In the command shown above, you would be a copying a file named “myscript” to the bin directory in your home directory. The bin directory will likely not exist unless you create it with a “mkdir ~/bin” command.
To delete a file, use the rm command. Deleting a file requires that you have write permission to it.
$ rm myfile
Wrap-up
Stay tuned for Part 2 of getting started on the Linux (or Unix) command line.
Copyright © 2023 IDG Communications, Inc.