1. 程式人生 > >Linux百問-001-020

Linux百問-001-020

001. apt-get upgrade vs apt-get dist-upgrade

apt-get upgrade will not change what is installed (only versions),

apt-get dist-upgrade will install or remove packages as necessary to complete the upgrade

upgrade

upgrade is used to install the newest versions of all packages
currently installed on the system from the sources enumerated in
/etc/apt/sources.list. Packages currently installed with new
versions available are retrieved and upgraded; under no
circumstances are currently installed packages removed, or packages
not already installed retrieved and installed. New versions of
currently installed packages that cannot be upgraded without
changing the install status of another package will be left at
their current version. An update must be performed first so that
apt-get knows that new versions of packages are available.

dist-upgrade

dist-upgrade in addition to performing the function of upgrade,
also intelligently handles changing dependencies with new versions
of packages; apt-get has a “smart” conflict resolution system, and
it will attempt to upgrade the most important packages at the
expense of less important ones if necessary. So, dist-upgrade
command may remove some packages. The /etc/apt/sources.list file
contains a list of locations from which to retrieve desired package
files. See also apt_preferences(5) for a mechanism for overriding
the general settings for individual packages.

002. How do I find all files containing specific text on Linux?

 grep -rnw '/path/to/somewhere/' -e 'pattern'
-r or -R is recursive,
-n is line number, and
-w stands for match the whole word.
-l (lower-case L) can be added to just give the file name of matching files.
Along with these, --exclude, --include, --exclude-dir or --include-dir flags could be used for efficient searching:

This will only search through those files which have .c or .h extensions:

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

This will exclude searching all the files ending with .o extension:

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

Just like exclude files, it’s possible to exclude/include directories through –exclude-dir and –include-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"

003. Delete empty folders in current directory

find . -type d -empty -delete
# User this only when you know exactly what you are doing

004. Change file line ending between unix and dos

unix -> dos

awk 'sub("$", "\r")' uniz.txt > windows.txt

dos -> unix

in vi: :1,$s/^M//g
[^M] is [Ctrl]+M

005. Switch GCC version

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 50

使用gcc-4.9替代原gcc版本,Priority=50

update-alternatives --display gcc
列出gcc命令的連結組

update-alternatives --remove name path,其中name與path與install中的一致,如果所刪除的連結組中還有其他連結的話,系統將會自動從其他中選擇一個priority高的連結作為預設為連結。

006. Run chrome from shell / open .html file in Chrome

google-chrome foo.html

007. File/folder comparison

sudo apt-get install meld
meld diff1.md diff2.md

008. List file size

du -h | sort -h
du -h -d 1
# -h for human readable size, like 1M, 4k
# -d 1 to search one level
## Check disk usage of directories
sudo du -sh ./*

(Output)
348K    ./_archive
940K    ./docs
1.5G    ./_RARs
1.6G    ./tracker_benchmark

009. Count file number in current directory

ls -1 | wc -l

# 使用-1 每行列出一個檔案
# wc -l 按行統計

010. Rename files in batch using regex

# using zsh command `zmv`
# run this in zsh
autoload zmv
zmv -w '*_*.mat' '$2_raw_$1.mat'

011. Kill processes with specific name

ps aux | grep Matlab | grep -v 'grep' | awk {print $2} | xargs kill -9

# grep -v to revert the meaning of matching 
# since the output of `ps aux` containing the grep process
# akw {print $2} to get the process PIE

# more short command to be learned

012. Clear/Cancel currernt line in terminal

# [Ctrl] + U to clear in current line
# [Ctrl] + C to cancel

013. How to manage packages

apt-cache search libffi

(Output)
libffi-dev - Foreign Function Interface library (development files)
libffi6 - Foreign Function Interface library runtime

sudo apt-get purge <package-name>

apt list --installed #列出安裝的apt包

dpkg -i foo.deb     #install
dpkg -l | grep foo      #check installation status
dpkg -r foo     #remove program, but not configuration 
dpkg -P foo     #remove all, including configuration

014. Copy multiple files to one destination folder

cp /home/usr/dir/{file1,file2,file3,file4} /home/usr/destination/
# https://askubuntu.com/questions/327282/copying-multiple-specific-files-from-one-folder-to-another

015. Mount removeable disk (e.g. TOSHIBA)

sudo mount -t ntfs-3g /dev/sdc1 media/tmp
# Not the ntfs format, use ntfs-3g instead

016. Create new user and grant privileges @mysql

# 1. If you don't want to change current validate_password_settings
show variables like 'validate_password%';
create user 'lcar979'@'localhost' identified by 'password here'
user mysql

GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
# The asterisks in this command refer to the database and table (respectively) 
# that they can access—this specific command allows to the user to read, edit, 
# execute and perform all tasks across all the databases and tables.


FLUSH PRIVILEGES;
# Once you have finalized the permissions that you want to set up for your 
# new users, always be sure to reload all the privileges.

# check users
select * from mysql \G

# 2. Or you can set current valdiate_password settings
set global validate_password_length = 6;
# then create new users

017. Hot to set default application

在你要設定的檔案型別的某個檔案上右鍵

選擇Properties-Open With

018. How to delete PPA

# Method 1. Use the --remove flag, similar to how the PPA was added:
sudo add-apt-repository --remove ppa:whatever/ppa

# Method 2. As a safer alternative, you can install ppa-purge:
sudo apt-get install ppa-purge
# And then remove the PPA, downgrading gracefully packages it provided
# to packages provided by official repositories:
sudo ppa-purge ppa_name
#Note that this will uninstall packages provided by the PPA, but not
#those provided by the official repositories. If you want to remove
#them, you should tell it to apt:
sudo apt-get purge package_name

# Method 3. You can also remove PPAs by deleting the .list files from 
# /etc/apt/sources.list.d directory.

Last but not least, you can also disable or remove PPAs from the “Software Sources” section in Ubuntu Settings with a few clicks of your mouse (no terminal needed).

019. How to disable zsh glob pattern alert

setopt nonomatch

020. 不鎖屏 & 螢幕快捷鍵

Brightness and lock 中設定不鎖屏
Shift + PrintScreen 選擇區域截圖

Extra

watch -n 10 nvidia-smi 每隔10s執行一次指令

fortune 隨機獲得一句英文諺語、名言、電影臺詞等

fortune-zh 獲得一句唐詩

(可以結合上面的watch一起用)

sudo !! 以sudo重新執行上一條命令

wget http://….. -q –show-progress
只顯示下載進度條