If you've begun to tinker with your desktop Linux terminal, you may be ready to take a deeper dive.
You're no longer put off by references to "terminal," "command line" or "shell," and you have a grasp of how files are organized. You can distinguish between a command, an option and an argument. You've begun navigating your system.
Now what?
Make It So
File manipulation -- that is, allowing users to traverse the directory structure and interact with its contents -- lies at the heart of Linux. There is a sizable arsenal of tools at your disposal. With the "mkdir" command, you can make new directories. The program takes one argument, which is a path ending with the name of the directory to be created.
$ mkdir new_directory
One nice thing about "mkdir" is that should you specify a directory that already exists, it won't overwrite the original.
Similarly, the "touch" command, taking a path to a file as an argument, can make new (empty) files.
$ touch file
As with "mkdir", "touch" does not harm existing files -- it merely updates the timestamp for its last access. This feature is very useful when making incremental backups, which only save files after a certain date, but "touch" is also great for making disposable files to practice on.
Removing files, using "rm", is a simple task as well, but you should approach it with caution. Supplying a path as an argument for "rm" will remove that file -- but once you hit Enter, it's gone forever. You won't find it in the Trash.
$ rm file
On the one hand, "rm" can't remove directories, so a slip of the keyboard won't cost you dozens of files. On the other, if you do want to remove a directory, and supply the "-r" option, there is no safety net, and the program will delete every directory it contains.
$ rm -r target_directory
If you want to give yourself the same breathing room with directories as "rm" affords regular files, you can use "rmdir", which returns an error and performs no action when the path leads to non-directories.
$ rmdir target_directory
To see file contents directly in terminal output, you can run "cat" with the file given as an argument.
$ cat /file
What's the advantage of using "cat" rather than a paging viewer like "less"? It works faster than "less", and you easily can return the contents of multiple files at once simply by adding more arguments.
$ cat file1 file2
Finally, you can edit files with a terminal text editor like "nano" or "vim". As with any other command, type in the name of your chosen editor with a path to the file and the terminal will bring up the contents in the editor interface. The "nano" editor is a good one to begin with if you're keen to start altering files, as it has usage instructions listed along the bottom.
Shell Basics
To make the most of these new commands in your discovery process, it helps to understand how the shell fits into the picture. Everything your computer runs must be in binary format, so when you type a command, how does the terminal know where the binary is?
The shell maintains an environment variable, a user- or system-wide value associated with a keyword (the variable name), called "PATH," and "PATH" lists all the directories where the shell should look for a command.
Every command, like everything in Linux, has a path, and if you want to know what it is, run the "which" command followed by the name of the command you want to find.
$ which command
You'll get the path right to the binary. Now try running "echo $PATH", which returns the value of the "PATH" variable.
$ echo $PATH
Odds are the directory containing the command you looked up with "which" was in there. This is why you don't have type out the path that "which" returns for every command you run.
There's more to the shell than that, however. To fine-tune its behavior and keep its users organized, the shell -- for Linux, Bash -- maintains a few configuration files. The first is ".bash_profile" or simply ".profile", depending on your Linux distrib
You're no longer put off by references to "terminal," "command line" or "shell," and you have a grasp of how files are organized. You can distinguish between a command, an option and an argument. You've begun navigating your system.
Now what?
Make It So
File manipulation -- that is, allowing users to traverse the directory structure and interact with its contents -- lies at the heart of Linux. There is a sizable arsenal of tools at your disposal. With the "mkdir" command, you can make new directories. The program takes one argument, which is a path ending with the name of the directory to be created.
$ mkdir new_directory
One nice thing about "mkdir" is that should you specify a directory that already exists, it won't overwrite the original.
Similarly, the "touch" command, taking a path to a file as an argument, can make new (empty) files.
$ touch file
As with "mkdir", "touch" does not harm existing files -- it merely updates the timestamp for its last access. This feature is very useful when making incremental backups, which only save files after a certain date, but "touch" is also great for making disposable files to practice on.
Removing files, using "rm", is a simple task as well, but you should approach it with caution. Supplying a path as an argument for "rm" will remove that file -- but once you hit Enter, it's gone forever. You won't find it in the Trash.
$ rm file
On the one hand, "rm" can't remove directories, so a slip of the keyboard won't cost you dozens of files. On the other, if you do want to remove a directory, and supply the "-r" option, there is no safety net, and the program will delete every directory it contains.
$ rm -r target_directory
If you want to give yourself the same breathing room with directories as "rm" affords regular files, you can use "rmdir", which returns an error and performs no action when the path leads to non-directories.
$ rmdir target_directory
To see file contents directly in terminal output, you can run "cat" with the file given as an argument.
$ cat /file
What's the advantage of using "cat" rather than a paging viewer like "less"? It works faster than "less", and you easily can return the contents of multiple files at once simply by adding more arguments.
$ cat file1 file2
Finally, you can edit files with a terminal text editor like "nano" or "vim". As with any other command, type in the name of your chosen editor with a path to the file and the terminal will bring up the contents in the editor interface. The "nano" editor is a good one to begin with if you're keen to start altering files, as it has usage instructions listed along the bottom.
Shell Basics
To make the most of these new commands in your discovery process, it helps to understand how the shell fits into the picture. Everything your computer runs must be in binary format, so when you type a command, how does the terminal know where the binary is?
The shell maintains an environment variable, a user- or system-wide value associated with a keyword (the variable name), called "PATH," and "PATH" lists all the directories where the shell should look for a command.
Every command, like everything in Linux, has a path, and if you want to know what it is, run the "which" command followed by the name of the command you want to find.
$ which command
You'll get the path right to the binary. Now try running "echo $PATH", which returns the value of the "PATH" variable.
$ echo $PATH
Odds are the directory containing the command you looked up with "which" was in there. This is why you don't have type out the path that "which" returns for every command you run.
There's more to the shell than that, however. To fine-tune its behavior and keep its users organized, the shell -- for Linux, Bash -- maintains a few configuration files. The first is ".bash_profile" or simply ".profile", depending on your Linux distrib
Comments
Post a Comment