Linux Commandline Tips
When using the Linux command line and related tools, there are many useful tricks and common practices worth noting. In this post, I’m keeping a running list of tips related to Linux command-line tools for future reference.
Table of Contents
Cd
cd -moves back to the previous directory.pushd <path>moves to the path and save the current directory.popdto return to the saved directory.
Command Line
ctrl-Amoves cursor to the beginning of line.ctrl-Emoves cursor to the end of the line.ctrl-Ucuts everything from cursor to beginning of the line.ctrl-Kcuts everything from cursor to end of the line.ctrl-Yyanks the last cut text.!!runs the previous command.
Cut
-c11-removes the first 11 chars of each line.-f2 -d':'extracts the second element of each line using ‘:’ as the delimiter.
Echo
echo $?displays the return value of the most recently run program.
Expand
expand -t 4 <filename>outputs the file with tabs expanded as 4 spaces.
Hexdump
hexdump -C <filename>dumps a binary file in canonical hex+ASCII format.
Git
Branch
- In case of ambiguity between file names and branch names (e.g. when both a file and a branch are named as
object).git checkout object --to checkout branch.git checkout -- objectto checkout file.
Gdb
- See https://sourceware.org/gdb/current/onlinedocs/gdb.html/ for a super detailed doc.
-x <filename>takes the gdb script and runs it in gdb.llists 10 lines of source code around the current program counter.l <n>lists 10 lines of source code around line n in the current source file.l mainlists 10 lines of source code around the start of functionmain.l <filename>:<n>: lists 10 lines of source code around line n in the specified source file.
tuican be triggered bylayout <format>.layout srcopens a tui window with source code.ctrl-X Atoggles tui on/off.ctrl-X Oswitches between tui windows.layout asmopens a tui window with assembly.layout regsopens a tui window with CPU registers.
Grep
-ndisplays line numbers.-cdisplays just the total number of matching lines.-Efor extended-regex pattern. For example,grep -E 'dsb|dmb|isb'.-vexcludes matched lines.-m <n>shows the firstnmatching lines.--exclude=".."excludes certain filenames.--exclude-dir=".."excludes certain directories.$''to include special charactors. For example,grep $'hello\tworld'.
Objdump
-zshows all symbols (to include those with zero values).- To perform objdump on arm binary,
- For bare-metal, install
binutils-arm-none-eabi, and usearm-none-eabi-objdump. - For arm64, install
binutils-aarch64-linux-gnu, and useaarch64-linux-gnu-objdump.
- For bare-metal, install
Ps
ps auxwfashows processes for all users.ushows the user/owner of each process.xshows processes not attached to a terminal.wshows wide output (to include the entire command line).fshows processes in a tree format.
Readelf
-xdumps bytes in a section in hexadecimal format. For examplereadelf -x .hyp.reloc vmlinux.
Set
- When writing a shell script, use
set -eat the beginning to specify that the script should exit immediately if any command exits with a non-zero status.
Tee
<command> | tee <filename>displays the output while saving it into the file.<command> | tee -a <filename>displays the output while appending it at the end of the file.<command> 2>&1 | tee <filename>displays the stderr and stdout while saving them in the file.- Shorthand:
<command> |& tee <filename>
- Shorthand:
Written on May 20, 2025