Contents

Command line tricks

Some command line tricks for Linux.

Passing arguments to a command from a text file

Using:

  • sed
  • xargs

For example, to install two lists of packages in Ubuntu:

1
cat list1.txt list2.txt | sed 's/#.*$//' | xargs sudo apt install
  • xargs takes the output from sed as arguments to apt
  • sed 's/#.*$//' filters out those lines after #. So the list2.txt can have comments like the following
1
2
3
4
5
# Comment

item1
item2  # A comment after an item
    item3  # indent supported

Passing multiple lines of string

Using heredoc to pass the string between two delimiters (e.g. EOF)

1
2
3
4
5
6
7
cat << "EOF" >> ~/.xprofile
# ~/.xprofile
export GTK_IM_MODULE=ibus
export QT_IM_MODULE=ibus
export XMODIFIERS=@im=ibus
ibus-daemon -drx
EOF

Will append the following lines in ~/.xprofile:

1
2
3
4
5
# ~/.xprofile
export GTK_IM_MODULE=ibus
export QT_IM_MODULE=ibus
export XMODIFIERS=@im=ibus
ibus-daemon -drx

nohup: Do not hang up in SSH sessions

Use nohup to run background process(es) uninterruptedly even when going offline in a remote SSH session.

1
nohup mycmd &

The output will be in nohup.out by default. If you want to customize the output location, just redirect it:

1
nohup mycmd &> log.txt &

You can also lower the priority for the background process

1
nohup nice mycmd &

When you’re done, you can kill the process by the proccess ID (PID)

1
2
3
ps -aux | grep "runoob.sh"

kill PID

Reference