LINUX ~ Terminal Shorthand

هنا توجد كل المواضيع المتعلقة بالبرمجيات المفتوحة المصدر

المشرف: Karam

LINUX ~ Terminal Shorthand

مشاركة غير مقروءةبواسطة Karam » الاثنين فبراير 07, 2011 3:36 pm

Whether you make a lot of typos, run a lot of the same commands over and over again, or you're dealing with long, annoying file paths, the Terminal has quite a bit of built-in shorthand to keep you from typing everything out yourself. Here are a few great examples.

File Paths
When you're working with files in the Terminal, navigating the folder structure of your system can take forever. Constantly typing long filenames is never fun, so here are a few tricks that'll get those 5-folder-deep paths into the Terminal with just a few taps of your mouse or keyboard.


Dragging Files Into the Terminal Window
If you have a file buried deep within your hard drive, typing out its full path can take forever. Instead of doing that, you can just open up the folder in Nautilus, Finder, Dolphin, or whatever file manager you use and drag the file right into the Terminal window. It will add its file path to the current command.


File Path Shorthand
We discussed these briefly in our beginner's guide, but they bear repeating. Say you're cding around your hard drive but don't want to type out file paths over and over again. If you need to continually access the same folders or files, dragging them in from your file browser can even get tedious. Luckily, you can substitute your current directory and its parent directory with . and .., respectively. For example, if you cd to a folder, running the following command will move you one folder up.
CODE: تحديد الكل
cd ..

That way, you don't have to retype the entire file path (or even hit the up arrow and delete a folder name—it's literally just a few keystrokes).

This also works if you're typing out longer paths. Say you're in ~/Documents/Work and you wanted to be in ~/Documents/Play. You could just type:
CODE: تحديد الكل
cd ../Play

and get there instantly.


Another good shortcut is the dash (-). This will move you back to your last working directory:
CODE: تحديد الكل
cd -


Thus, if you're working in, say, your documents folder (~/Documents) and moved over to the /etc/ briefly, you could switch right back by typing cd - and hitting Enter.

Lastly, if you want to go back to your home directory, there's no need to add any arguments to the cd command. Just typing cd and hitting enter will bring you back home.


Using Your History

Accessing recently used commands (or running a small variation of recently used commands) is something Terminal users often need to do. Unfortunately, it's also one of the most tedious parts of the Terminal—who wants to retype the exact same command they just ran but with one minor difference? It makes every typo seem like a punishment. Luckily, there's some pretty nice shorthand, most of which uses the handy bang symbol (!).

sudobangbang.png


One of the most useful shortcuts is using !! to represent the last command you ran. This is useful in a ton of situations. For example, if you run a command that needs root privileges but forget to add sudo to the beginning, there's no need to retype the command. Just run:
CODE: تحديد الكل
sudo !!

This will run your last used command with root privileges.



printlastcat.png

If the command you want to run is a bit further back in your history, you can use the bang in conjunction with the original string to find it. For example, if you want to run the last command that used cat, you could just type:
CODE: تحديد الكل
!cat



If you just want to see what the last cat command was, you can instead run:
CODE: تحديد الكل
!cat:p

This will print that command and add it to the end of your history. If you decide you then want to run it, you can just type !! and hit Enter.

If you want to run a different command that you ran last, but with the same argument, there's a shortcut for that too. For example, say you had just created a folder using:
CODE: تحديد الكل
mkdir /new/awesome/folder


To then cd into that directory, you could just type:
CODE: تحديد الكل
cd !$

The !$ represents the arguments from your last command.


Another common problem is mistyping the command you want to run. Say you wanted to run nano, but accidentally typed nanp:
CODE: تحديد الكل
nanp /path/to/a/document/buried/deep/in/the/filesystem



Instead of retyping the whole thing, you could just run:
CODE: تحديد الكل
^nanp^nano



This will find the first instance of nanp in the last run command and replace it with nano.

historygrep.png


While all these shortcuts are fine and dandy, but it's worth mentioning that the history command is your friend. If you want to see all the recent commands you ran that included nano, for example, you could just run:
CODE: تحديد الكل
history | grep nano

You'll get a list that looks something like this:
CODE: تحديد الكل
381 sudo nano /etc/NetworkManager/nm-system-settings.conf
387 sudo nano /etc/rc.conf
388 sudo nano /etc/rc.conf
455 sudo nano /boot/grub/menu.lst


You can then pick a command out from that list—say I want to run sudo nano /boot/grub/menu.lst, which grep lists as command 455—and run it using:
CODE: تحديد الكل
!455

Lastly, if you want to keep certain commands out of your history, just put a space before them—i.e. space+nano ~/Documents/WorldDominationPlans.txt.



Expansions

When you're working with variations of a file—like backups or different file types—it can get tedious typing out the same commands with small tweaks. Using the brace symbols ({}), you can easily perform batch operations on multiple versions of a file.

Say you want to rename just part of a filename. Instead of typing out mv /path/to/file.txt /path/to/file.xml, you could just run:
CODE: تحديد الكل
mv /path/to/file.{txt,xml}


This runs the command with the same arguments, only with the parts inside the brace changed—the first part corresponding to the first argument, the second part corresponding to the second argument.

The most common example of this is when you're backing up a file that you're making changes to. For example, if you are tweaking your rc.conf, you'll want to make a backup in case the new one doesn't work. So, to do so, you can just run:
CODE: تحديد الكل
sudo cp /etc/rc.conf{,-old}


Putting nothing before the comma will just append -old to the filename after copying it with cp. If your new file doesn't work out and you want to restore the backed up file to its original location, you can just use:
CODE: تحديد الكل
sudo mv /etc/rc.conf{-old,}


Moving the comma to the other end of the brace will remove -old from the end of the file and restore it to its original name.

The braces can also work when moving or creating multiple files at once. For example, if you wanted to create three numbered directories, you could just run:
CODE: تحديد الكل
mkdir myfolder{1,2,3}

This will create three folders: myfolder1, myfolder2, and myfolder3.



Making Your Own Shorthand

bashrc.png


While these are all pretty handy, the most useful thing you can probably do is make up your own shortcuts. After all, we all have a few commands we run over and over again, but they aren't necessarily the same for everyone. To do so, we're going to edit the ~/.bashrc configuration file (or, if you're on Mac OS X Snow Leopard, ~/.bash_profile). It's a basic text file, so you can do it with whatever you like—Gedit in Ubuntu, TextEdit on OS X, or even nano within the Terminal. We've talked about doing this a few times before, but it really is one of the best things you can do to speed up Terminal work.

To create a custom shortcut (called an alias or function, you'll want to add a new line to your .bashrc file using the following format:
CODE: تحديد الكل
alias la='ls -A'


Now, whenever you type la, the Terminal will run ls with the -a modifier, which includes hidden files. Some of thsee are built into popular Linux distributions already, but there are a ton of other useful ones. Here are some of our favorites:
CODE: تحديد الكل
alias ll='ls -l'


This gives you a more verbose list of files than ls does on its own. In Ubuntu, this shortcut already exists, but runs ls -alF.
CODE: تحديد الكل
alias desk='cd ~/Desktop'


This will make your Desktop the working directory with just a few keystrokes. You can, of course, modify this for pretty much any folder that you access regularly.
CODE: تحديد الكل
alias up='cd ..'


This moves your working directory one folder up in half the keystrokes.
CODE: تحديد الكل
alias emenu='aterm nano -e ~/.e16/menus/user_apps'


This is an example of an alias that opens up a file for editing. If you have any files you find yourself constantly editing, this is a good one to keep around—just throw the path to your oft-used file in the quotes and edit the keyword to something that makes sense.

CODE: تحديد الكل
alias agi='sudo apt-get install'


With this, installing programs is much quicker in Ubuntu. You can just type agi chromium to install Chromium, for example. Of course, if you're using a different flavor of Linux, you can replace it with your package manager of choice.

CODE: تحديد الكل
alias update='sudo apt-get update'


This will update all your packages in Ubuntu.

CODE: تحديد الكل
function cdl { cd $1; ls;}



This is a neat function we've featured before that will essentially run cd and ls at the same time. So, just type cdl /path/to/folder and the Terminal will both make that your working directory and list its contents in one fell swoop.
أنا بتنفس حرية .. ما تقطع عني الهوى
صورة العضو الشخصية
Karam
مشرف منتدى البرمجيات المفتوحة المصدر
مشرف منتدى البرمجيات المفتوحة المصدر
 
مشاركات: 2039
اشترك في: الخميس أكتوبر 01, 2009 2:50 pm
مكان: Stadt Ilmenau, Thuringen, Germa
الجتس: ذكر
الشهادة الثانوية: Syria
الجامعة: Damascus
الكلية: ITE
المرحلة الدراسية: السنة الرابعة
الاختصاص: شبكات و نظم

العودة إلى منتدى البرمجيات المفتوحة المصدر

الموجودون الآن

المستخدمون المتصفحون لهذا المنتدى: لا يوجد أعضاء مسجلين متصلين و 1 زائر

cron