Knowing a few key shortcuts can have a surprisingly positive impact on your productivity. As someone who sits frequently with others over a command line to help debug code and to navigate servers, it's easy to become frustrated on the typer's behalf. Working at the command line can be cumbersome when you don't know the best time-saving shortcuts to use. This article fixes that frustration for you.
Here are some essential shortcuts and key tips to help you speed up your command line usage.
Run that again as root — sudo
$ cat /var/log/messages
cat /var/log/messages: Permission denied.
Don’t use bash history with keystrokes: up arrow, left arrow, left arrow, left arrow, sudo, ENTER.
Instead use: sudo !!
This little shortcut works because !! is a shell place holder for the last command executed. Typing those seven characters will run that last command as root without those up and left arrow key presses. This shortcut also works without sudo, if you want to run the last command again without changes, for some reason.
Search for that command I ran — Ctrl+r
What was that command I ran? Up arrow, up, up, up. Oh, there it is. ENTER.
You search through your history one step at a time but there's a better way. What if I told you there is an easy way to search your previous commands?
Don’t type: Up arrow, up, up, up, ENTER.
Instead: Ctrl+r
Note: Don't type the (+). This means to use the Ctrl key and the r key together.
Simply use, Ctrl+r, and type the first few letters of the command you want to repeat. If the search doesn’t match on the first result, just use Ctrl+r a few more times to scroll through results—shown below searching for the cat command.
(reverse-i-search)cat: sudo cat /var/log/messages
Go back to your home directory — cd
You would be amazed at how many people don’t know this. cd. That’s right. Without any arguments, it takes you back to your home directory.
Go back to the last directory - cd -
Sometimes the simplest things are the best. Where you in the /var/www/foo directory, but are now in /etc? Simply type, cd - and you will return to /var/www/foo .
Don’t type: cd /var/www/foo
Instead: cd -
Job control — backgrounding, foreground, etc
This might take some getting used to, but when you get the hang of it you’ll never go back. Let’s say you are editing a file in vim (well, you wouldn’t use nano, would you?), and now you want to go and look in the /var/www/html directory. You could quit vim, browse to the directory, only to find that you want to edit the file again. Instead, you can send vim to the background and come back to it later.
Type: Ctrl+z — This is a shortcut that backgrounds any existing foreground task. Useful for, but not limited to, less , cat , man , vim , etc.
Where did my foreground task go, you might ask. Simply type, jobs to see it in a list.
$ jobs
[1] Stopped vim
Great. You can now go do something else. Whenever you want this back again, simply, type fg . This will bring the background job (vim) back to the foreground again. Note that the background process is paused, so if you’re running something like tail on a file, the process will have some catching up to do. If you have multiple jobs running in the background, fg 3, for example, resumes the third job in the list. Don’t forget to run the jobs command to see a list of paused jobs.
Alias frequently used commands
If you often run a command with the same arguments, create an alias for it. I have many of them. I often use the syntax, which is the command's normal name followed by an x. For example, with netstat, I always run it with -n (numeric addresses only) , -t (tcp protocol), -a (all), -u (udp protocol), and -e (extended output). netstat -ntaupe — it rolls right off the tongue, right? I’m lazy (and might forget an option), so I aliased that to netstatx like this;
$ alias netstatx="netstat -ntaupe"
Try it for anything you run regularly.
Don’t type: netstat -ntaupe
Instead: netstatx
A bonus shortcut
You can use the keyboard combination, Alt+., to repeat the last argument.
Note: The shortcut is Alt+.(dot).
$ mkdir /path/to/mydir
$ cd Alt.
You are now in the /path/to/mydir directory.
Summary
Invest a little time now to save time in the future will really speed up your life at the command line.
Want to test your sysadmin skills? Take a skills assessment today.
저자 소개
James Read is a Principal Solution Architect for EMEA Cloud and Service Providers and has been at Red Hat since 2011. A technologist at heart, James is passionate about code, containers, and cloud.
유사한 검색 결과
Behind the scenes of RHEL 10, part 3
Alliander modernises its electricity grid with Red Hat for long-term reliability in balance with rapid innovation
Linux, Shadowman, And Open Source Spirit | Compiler
The Overlooked Operating System | Compiler: Stack/Unstuck
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래