Managing Documents or Files with the Linux terminal command

Working at the command line is a brief opportunity to clicking via filesystem control tasks. Here are a few fundamentals to get you started.

If you are acquainted with navigating your laptop in a Linux terminal, you then definitely already understand how green a terminal can be. Instead of 8 clicks to get to a subdirectory of a subdirectory in a directory, you may kind one command and all however teleport immediately in your destination.

Managing documents or file with the Linux terminal

Managing documents or file with the Linux terminal

For people who need to shine their skills, or are hesitant to permit cross in their GUI, this newsletter demonstrates the way to control and engage with documents out of your terminal. Unlike conventional tutorials that concentrate on coaching you instructions, this newsletter takes a task-primarily based totally approach, coaching you the way to do everyday matters and offering you with the applicable instructions as you cross.

Create a file

It’s beneficial to have documents to control as you comply with along. There are masses of documents already to your system, however a lot of them are essential and pleasant omitted of exercise sessions. Instead, you may create your personal take a look at documents.

The echo command is a simple program that echoes whatever argument you provide. For example: $ echo hello hello You can leverage shell redirects (>) to send the output of nearly any command (echo included) into a file. The first version of the redirects below overwrites any content currently in a file, while the second appends new content to the end of the file. If the redirect file doesn’t exist, the shell creates it. Try this: $ echo hello > ~/Documents/hello.txt $ echo world >> ~/Documents/hello.txt With the file hello.txt now located in your Documents folder, open it in a text editor to see the results. The file contains two lines, reflecting your echo statements. Generally, it's safer to use two redirects as done above, instead of just one, since one redirect overwrites content. For example, the following does something different than the previous series of commands: $ echo hello >> ~/Documents/world.txt $ echo world > ~/Documents/world.txt Open the world.txt file in a text editor to see the result.

Move a file

In a GUI, open the folder containing the file. Open a second window to the folder you want to move the file to. Drag and drop the file from the first window to the second. At the command line, the mv command moves a file from one place to another. As long as you know where you want to take a file from and where you want to move it to, you can send files flying all over the place, no matter where you are in the filesystem. It’s a huge time-saver compared to navigating through your files in a series of windows to locate the one you want, then opening a new series of windows to get where you want that file to go, and then moving the file. Suppose you downloaded a Git cheatsheet using a web browser. The typical default location for downloads is, understandably, your ~/Downloads folder—the ~ refers to the account you're currently logged into's home directory. The following example shows how you to move this file from ~/Downloads to your collection of cheat sheets at ~/Documents/Cheatsheets: $ pwd /home/seth $ ls ~/Downloads $ ls ~/Documents Cheatsheets $ mv ~/Downloads/ ~/Documents/Cheatsheets $ ls ~/Documents/Cheatsheets
When using filesystem commands, you can use absolute (exactly where to operate in the filesystem) file paths or relative file paths (where to operate in the filesystem relative to where you are.) For example, if you're moving a file from ~/Downloads to ~/Documents/cheatsheet while your terminal prompt is inside ~/Documents/cheatsheet, you can use the relative notation since it's shorter. To do so, use a dot (.) to indicate that the file should move to wherever your terminal prompt is currently located, as shown below: $ pwd /home/seth/Documents/Cheatsheets $ mv ~/Downloads/ . $ ls
By default, the mv command does not ask whether you want to overwrite a file with the same name as the one you are moving. To protect yourself from this, use mv --interactive (or mv -i), which forces mv to request permission before overwriting an existing file that has the same name as this new one.

Rename a file

In a GUI, open a window and find the file you want to rename. Click on the file's name or right-click on it to select the rename option. Enter a new name. At the command line, the mv command also "moves" a file from itself to itself, with a new name. One way to rename a file in Linux is by using the mv command, which can "move" a file from itself to itself, but with a new name. For example: $ ls ipv4.list $ mv ipv4.list ipv6.list $ ls ipv6.list
Because both renaming and moving use the same command, you can actually combine them into one action. To move a file from one folder to another, and also rename it in the process, provide the new file name in your move command like so: $ mv ~/Downloads/ip-address.txt ~/Documents/assets/ipv4.list $ ls ~/Documents/assets/ ipv4.list

Copy a file

In a GUI, open a series of windows to find the file you want to copy, then open a second window to reach the new destination. Press a modifier key and drag a file from one window to another. At the command line, the cp command makes a copy of a file and its contents. The cp command works exactly like mv, except that it duplicates the file's contents instead of moving the file from one location to another. For example: $ ls ~/Downloads $ ls ~/Documents Cheatsheets $ cp ~/Downloads/ ~/Documents/Cheatsheets $ ls ~/Downloads $ ls ~/Documents/Cheatsheets
As with the mv command, the cp command by default overwrites any document with the same name in the destination folder. To avoid that behavior, use cp --interactive (or cp -i) like this: $ cp --interactive ~/Downloads/ ~/Documents/Cheatsheets cp: overwrite ‘./’? n
To copy a file into the same directory, give it a new name during the copy operation. For example: $ cp ~/Downloads/ ~/Documents/Cheatsheets/ $ ls ~/Downloads $ ls ~/Documents/Cheatsheets
Try copying some of the files you've created for this exercise, and renaming them as needed. If you want your shell to provide you with feedback, add the --verbose (or -v) option to your command.

Copy a folder

Folders don't really exist in the Linux filesystem, at least not in the way you imagine them. A folder is just a directory listing of all the files it "contains." To copy a folder, use cp --recursive (or cp -r), which copies all of the files in a folder as well as the folder designation itself. Here's an example: $ ls --classify people/ $ cp people ~/Documents cp: omitting directory 'people/' $ cp --recursive people ~/Documents $ ls --classify ~/Documents people/

Create a folder

In a GUI, open a series of windows to reach the new folder's location, then click a button or menu to create a new folder. Create a folder anywhere in your file system with the mkdir command. At the command line, to create a folder anywhere in your file system, use the mkdir (make directory) command. You can create a folder in your current filesystem location just by providing a name. For example: $ mkdir newfolder $ ls newfolder
You can create a folder in any other filesystem location by specifying the where it should go using an absolute or relative file path. This example uses the absolute path: $ mkdir ~/Documents/newfolder $ ls --classify ~/Documents Cheatsheets/ people/ newfolder/

Create a shortcut

In a GUI, find a file you need a shortcut to and select the option to create a shortcut (or link or alias, depending on the OS) from a contextual menu. The command line has even more to offer here. One of the most powerful features computers offer is the ability to have a file or folder in more than one place at once. If you make two copies of a paper file or folder, you're in danger of making changes to one and forgetting to make the same change to the other. This issue isn't a problem with symbolic linking (symlink for short) in Linux. To create a shortcut, use the ln command as you see below (you can use ln -s instead of ln --symbolic): $ ls ~/Documents oncall-schedule.txt $ ln --symbolic ~/Documents/oncall-schedule.txt ~/Desktop/oncall-schedule
Should you forget which file is a symlink and which is the actual file, use the file command. For example: $ file ~/Desktop/oncall-schedule /home/seth/Desktop/oncall-schedule: symbolic link to `/home/seth/Documents/oncall-schedule.txt'
To identify symlinks at a glance, you can invent your own convention, such as a naming scheme for symbolic links that includes the word link, as in this example: $ ls ~/Documents oncall-schedule.txt $ ln --symbolic ~/Documents/oncall-schedule.txt ~/Desktop/


Switching to the Linux command line to manipulate your filesystem can take a few being used to, however it is really well worth the effort. The in addition you dig into the command line's capabilities—specially while handling your filesystem—the quicker you will get matters done. Start through working towards those simple techniques, after which dig deeper.
Next Post Previous Post
No Comment
Add Comment
comment url