Skip to content

Instantly share code, notes, and snippets.

@rupeshtiwari
Last active March 13, 2026 02:46
Show Gist options
  • Select an option

  • Save rupeshtiwari/6e9dd71e75c775e2e6f16ce9aeb3988c to your computer and use it in GitHub Desktop.

Select an option

Save rupeshtiwari/6e9dd71e75c775e2e6f16ce9aeb3988c to your computer and use it in GitHub Desktop.
unix linux commands bash scripts, run in background, kill process in linux

Linux commands

How to know which process is using port

# port 9600 
sudo lsof -i :9600

image

How to kill a process

sudo kill 41789

How to run .sh file

sh index.sh

How to see all processes running and kill them

# check processes 
ps -aux|grep <process-name>

# kill a process
kill -9 <process-id>
# check all fluent-bit process
ps -aux|grep fluent-bit

image

# kill fluent-bit instance
kill -9 <process-id>

image

# note process is killed

image

Sample .sh file

Create new file index.sh add below code

#!/bin/sh
echo "Hello World"

Give file permission to execute file

sudo chmod +x ./index.sh

Run script on background

sudo nohup ./index.sh &

Rename a file

mv oldfile.txt newfile.txt

Delete directory

Use the rmdir or rm -d command to remove empty directories. Use the rm -r command to remove non-empty directories.

Delete everything except one folder

delete all except ad-demo folder

ls /data/ | grep -v "ad-demo" | xargs rm -r

Download Tar File

curl -s https://artifacts.elastic.co/downloads/logstash/logstash-8.7.1-linux-x86_64.tar.gz | tar xz

copy folder

cp -R path_to_source path_to_destination/

copy file to another folder

cp ./DirectoryA_1/README.txt ./DirectoryA_2

Create .sh or bash fiile

add this line on the top of the .sh file #!/bin/bash

How do I shorten the current directory path shown on terminal?

PROMPT_DIRTRIM=1

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment