Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

How to change the size of swapfile in Linux



A swap file is a file that contains data retrieved from system memory, or RAM. By transferring data from RAM to a secondary storage device in the form of a swap file, a computer is able to free up memory for other programs [1]. 

Sometimes, a large swap file can help when your RAM size is limited, e.g., training a large Deep Learning model on your laptop (although it is not a good idea...)

In Linux, we can follow the several steps in the following to change the swap size.

1. disable the current swap file

$ sudo swapoff /swapfile

2. increase (or decrease) the size of swap file (e.g., 8G as below)

$ sudo dd if=/dev/zero of=/swapfile bs=1G count=8

3. Setup the file as our swap file

$ sudo mkswap /swapfile

4. enable swaping

$ sudo swapon /swapfile

5. check the current swap size

$ grep SwapTotal /proc/meminfo


References

Useful Linux commands



File Manipulation


# How to copy a file? e.g., top-words-2.sh to top-words-3.sh 

cp -v top-words-2.sh top-words-3.sh
cp -v top-words-{2,3}.sh

# How to zip a folder into a .zip file? e.g., we want to create a .zip file called zipfilename.zip by zipping all files in the folder named folder_name

zip -r zipfilename.zip folder_name


Search


# How to search files containing a certain str?

grep -iRl "str to search" ./

-i - ignore text case
-R - recursively search files in subdirectories.
-l - show file names instead of file contents portions.


grep -rnw '/path/to/somewhere/' -e 'pattern'

# only search through those files which have .c or .h extensions

grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

# exclude searching all the files ending with .o extension

grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"

# exclude a particular directory(ies) through --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/

grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"




# today's file at current directory with certain regex

find . -maxdepth 1 -mtime 0 -name "*hello*"

# change multiple file names, e.g., *.txt files to *.text

for f in *.txt;
do mv -- "$f" "$(basename "$f" .txt).text"
done

# find and remove files

# first command remove directories as well
find . -name "FILE-TO-FIND" -exec rm -rf {} \;
# second command only removes files
find . -type f -name "FILE-TO-FIND" -exec rm -f {} \;

# how to delete all files with a certain extension in current directory & subdirectories?

check only:        find . -name "*.bak" -type f
delete as well:    find . -name "*.bak" -type f -delete

# how to delete files that are not belonging to a certain pattern?

find . -type f ! -name '*.txt' -delete

# search process id using certain port (e.g., 8000)

lsof -ti:8000
netstat -tulpn | grep ':443'

Others


# start default vino server in Ubuntu

/usr/lib/vino/vino-server

# clean trash bin

sudo apt install trash-cli
trash-empty

# get full permission to a file/folder
sudo chmod a+rwx /path/to/file
sudo chmod -R a+rwx /path/to/folder

# check status/start/stop/restart a service

sudo systemctl status [service name]
sudo systemctl start [service name]
sudo systemctl stop [service name]
sudo systemctl restart [service name]

Keep process running after ending the ssh session using "screen"

Simple scenario from askubuntu:
  • ssh into your remote box. Type screen Then start the process you want.
  • Press Ctrl-A then Ctrl-D. This will "detach" your screen session but leave your processes running. You can now log out of the remote box.
  • If you want to come back later, log on again and type screen -r This will "resume" your screen session, and you can see the output of your process. However, if you have multiple screens have been detached before, you might get this below:





  • In this case, just specify the screen you want to re-attach
    • screen -r 174044.pts-3.srvgal107
  • Kill screen session : Press Ctrl-A then K.
  • Access attatched screen : screen -D -r '1234.somescreensession'
  • Scroll inside screen
    • Maybe there's a better way, but I'm used to scrolling using the "copy mode" (which you can use to copy text using screen itself, although that requires the paste command too):
      • Hit your screen prefix combination (C-a / control+A by default), then hit Escape.
      • Move up/down with the arrow keys (↑ and ↓).
      • When you're done, hit Return twice to get back to the end of the scroll buffer.
      (If you move the cursor after hitting Return once, you'll be selecting text to copy, and hitting Return the second time will copy it. Then you can paste with C-a followed by] )
  • Quite a session: screen -X -S [session # you want to kill] quit