Abstract This article recorded some useful fundamental commands that I came across at the beginning of my linux learning.
第一章 Linux 文件管理
目录结构
列表显示 ls
打开目录 cd
查看文档 cat
创建新文件或心目录
删除命令 rm
改名与移动 mv
压缩与解压 tar
查找文件 find
查找文件内容 grep
修改文件权限 chmod
目录结构
名称
解释
/
根目录
bin
放普通用户的命令
boot
linux的核心文件 如mrb镜像 grub启动
dev
硬件 如串口
etc
系统配置文件 如config
lib
存放动态库
mnt
挂载不同设备
proc
存放cpu 内存 硬件信息
sbin
存放超级用户命令
tmp
临时名录 放置临时信息
usr
类似windows的program file 内有src(source),bin,sbin
var
存放变动性信息 log spool cache
绝对路径: 以/起始 如/home/user1/abc.txt 相对路径: user1/abc.txt
列表显示 ls
命令
解释
ls -l
详细列出所有文件, 一般用 ll 也可以
ls -a
显示所有文件 包括隐藏文件
ll -h
-h 表示show in a human-friend way
linux中 所有以点开头的文件都是隐藏的文件
TAB 敲两次 出现所有可能结果
Linux命令执行机制:翻译+执行 如ls 系统先执行ls=ls –color 然后在执行ls –color alias ls=ls可设置回来 再比如 alias ubuntu=ls 这样执行ubuntu这条命令时 系统会把ubuntu解释成ls
查找某一命令的详解,使用man,比如
1 2 3 4 5 6 7 8 9 $ ll -h total 228K drwx------ 10 lin lin 4.0K Mar 22 15:29 ./ drwxrwxr-x 4 lin lin 4.0K Mar 5 14:57 ../ drwx------ 2 lin lin 4.0K Mar 21 15:21 android/ -rw-rw-r-- 1 lin lin 18K Mar 17 16:17 break_vigenere.md drwxrwxr-x 2 lin lin 4.0K Mar 19 22:56 computer_system/ -rw------- 1 lin lin 463 Mar 5 14:13 France.md drwx------ 2 lin lin 4.0K Mar 5 14:13 French_Learning/
第一列:d表示目录 -表示文件 第二到第九十列:r读 w写 x执行 -表示对应的rwx无权限 第一个rwx表示文件所有者权限,第二个表示组权限 第三个表示其他用户权限 第一个lin表示用户 第二个lin表示用户所在组 后面数字表示文件或是目录大小
打开目录 cd
命令
解释
cd
回到home目录
cd ..
回到上层目录
cd -
回到之前所在目录
pwd
显示当前目录
./ 表示当前目录 ../ 表示上一级目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ pwd /home/lin $ cd /usr/local/MATLAB/ $ pwd /usr/local/MATLAB/ $ cd - /home/lin $ pwd /home/lin $ cd .. $ pwd /home $ cd $ pwd /home/lin
查看文档 cat
命令
解释
cat filename
在终端查看文档
head filename
在终端查看文档,前十行
head -n filename
在终端查看文档,前n行
tail filename
在终端查看文档,后十行
tail -n filename
在终端查看文档,后n行
cat filename_1 >> filename_2
将文档输出至filename_2的末尾
cat filename_1 > filename_2
将文档输出至filename_2(覆盖)
1 2 3 4 5 6 7 $ head -2 hello.txt hello, this is line 1 hello, this is line 2 $ tail -2 hello.txt hello, this is line 11 hello, this is line 12
创建新文件或新文件夹
命令
解释
touch filename
创建名为filename的新文档
mkdir foldername
创建名为foldername的新文件夹
mkdir -p folder/new_folder1/new_folder2
创建多级新文件夹
1 2 3 4 5 6 7 8 9 10 $ mkdir hello $ cd hello/ $ pwd /home/lin/Dropbox/hello $ ls $ mkdir -p hello_01/hello_02 $ ls hello_01 $ ls hello_01/ hello_02
删除命令 rm
命令
解释
rm filename
删除某一文件
rm -r foldername 或rmdir
删除某一文件夹
rm *
删除当前目录下所有文件
1 2 3 4 5 6 7 8 9 10 $ ll total 24 drwxrwxr-x 3 lin lin 4096 Mar 22 15:59 ./ drwxrwxr-x 3 lin lin 4096 Mar 22 15:59 ../ drwxrwxr-x 2 lin lin 4096 Mar 22 15:59 hello_02/ $ rm -r hello_02/ $ ll total 16 drwxrwxr-x 2 lin lin 4096 Mar 22 16:04 ./ drwxrwxr-x 3 lin lin 4096 Mar 22 15:59 ../
改名与移动 mv
命令
解释
mv filename_1 filename_2
将filename_1文件重命名为filename_2
mv filename_1 ../filename_2
将filename_1文件移动至上层目录并重命名为filename_2
1 2 3 4 5 6 7 8 9 10 11 $ touch hello.txt $ mv hello.txt ../hello_02.txt $ ls hello.txt ls: cannot access hello.txt: No such file or directory $ ls ../hello_02.txt ../hello_02.txt $ mv ../hello_02.txt ./hello.txt $ ls ../hello_02.txt ls: cannot access ../hello_02.txt: No such file or directory $ ls hello.txt hello.txt
压缩与解压 tar
命令
解释
tar -zcvf {.tgz-file} {files}
将{file}压缩为{.tgz-file},c代表compress
tar -zxvf {.tgz-fiile}
将{.tgz-file} 解压到当前文件夹
1 2 3 4 5 $ ls AITR-720.pdf AITR-720.pdf $ tar -zcvf test.tar.gz AITR-720.pdf $ ls test.tar.gz test.tar.gz
查找文件 find
命令
解释
find {folder} -name “string “
在{folder}目录下查找文件名含有string的文件
1 2 3 4 $ find ./ -name "*android*" ./android ./android/android_forum.md ./android/android_theory.md
查找文件内容 grep
命令
解释
grep “string” ./*
在当前目录下(不含子目录)查找含有string内容的文件
grep -r “string” ./*
在当前目录下(含子目录)查找含有string内容的文件
grep -rn “string” ./*
在当前目录下(含子目录)查找含有string内容的文件,并打印行号
1 2 $ grep -rn "hello" test/* 1:this is an example of grep, i want to find "hello"
修改文件权限 chmod 这里先复习以下ls命令1 2 3 4 5 $ ll total 12 drwxrwxr-x 2 lin lin 4096 Mar 22 22:24 ./ drwxr-xr-x 4 lin lin 4096 Mar 22 22:25 ../ -rw-rw-r-- 1 lin lin 66 Mar 22 22:24 hello.txt
第二到第九十列:r读 w写 x执行 -表示对应的rwx无权限 第一个rwx表示文件所有者权限,第二个表示组权限 第三个表示其他用户权限。若用二进制加以区分不同的权限的话,111(即十进制的7)代表可读可写可执行,110(即十进制的6)代表可读可写但是不可执行,以此类推。
命令
解释
chmod 权限值 文件名
给文件赋以相应的权限,如chmod 666 hello.txt,表示所有用户都可对hello.txt进行读写操作
1 2 3 4 5 6 $ chmod 666 hello.txt $ ll total 12 drwxrwxr-x 2 lin lin 4096 Mar 22 22:24 ./ drwxr-xr-x 4 lin lin 4096 Mar 22 22:25 ../ -rw-rw-rw- 1 lin lin 66 Mar 22 22:24 hello.txt
第二章 Linux 信息查询 查看计算机信息 查看cpu信息:sudo cat /proc/cpuinfo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 $ sudo cat /proc/cpuinfo [sudo] password for lin: processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 23 model name : Intel(R) Core(TM)2 Duo CPU T6670 @ 2.20GHz stepping : 10 microcode : 0xa07 cpu MHz : 2201.000 cache size : 2048 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 2 apicid : 0 initial apicid : 0 fdiv_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm ida dtherm tpr_shadow vnmi flexpriority bogomips : 4400.21 clflush size : 64 cache_alignment : 64 address sizes : 36 bits physical, 48 bits virtual power management: processor : 1 vendor_id : GenuineIntel cpu family : 6 model : 23 model name : Intel(R) Core(TM)2 Duo CPU T6670 @ 2.20GHz stepping : 10 microcode : 0xa07 cpu MHz : 1200.000 cache size : 2048 KB physical id : 0 siblings : 2 core id : 1 cpu cores : 2 apicid : 1 initial apicid : 1 fdiv_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm ida dtherm tpr_shadow vnmi flexpriority bogomips : 4400.21 clflush size : 64 cache_alignment : 64 address sizes : 36 bits physical, 48 bits virtual power management:
查看内存:sudo cat /proc/meminfo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 $ sudo cat /proc/meminfo MemTotal: 4148372 kB MemFree: 2393300 kB MemAvailable: 2967936 kB Buffers: 108336 kB Cached: 678276 kB SwapCached: 0 kB Active: 1135536 kB Inactive: 434344 kB Active(anon): 793392 kB Inactive(anon): 41728 kB Active(file): 342144 kB Inactive(file): 392616 kB Unevictable: 672 kB Mlocked: 672 kB HighTotal: 3284648 kB HighFree: 1934244 kB LowTotal: 863724 kB LowFree: 459056 kB SwapTotal: 4193276 kB SwapFree: 4193276 kB Dirty: 108 kB Writeback: 0 kB AnonPages: 783936 kB Mapped: 354852 kB Shmem: 51856 kB Slab: 80168 kB SReclaimable: 59848 kB SUnreclaim: 20320 kB KernelStack: 3736 kB PageTables: 12036 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 6267460 kB Committed_AS: 4394972 kB VmallocTotal: 122880 kB VmallocUsed: 52848 kB VmallocChunk: 38164 kB AnonHugePages: 231424 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB DirectMap4k: 108536 kB DirectMap2M: 800768 kB
查看分区:sudo fdisk -l 1 2 3 4 5 6 7 8 9 10 11 12 13 $ sudo fdisk -l Disk /dev/sda: 320.1 GB, 320072933376 bytes 255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000bd319 Device Boot Start End Blocks Id System /dev/sda1 * 2048 616753151 308375552 83 Linux /dev/sda2 616755198 625141759 4193281 5 Extended /dev/sda5 616755200 625141759 4193280 82 Linux swap / Solaris
查看显卡:lspci 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 $ lspci 00:00.0 Host bridge: NVIDIA Corporation MCP79 Host Bridge (rev b1) 00:00.1 RAM memory: NVIDIA Corporation MCP79 Memory Controller (rev b1) 00:03.0 ISA bridge: NVIDIA Corporation MCP79 LPC Bridge (rev b2) 00:03.1 RAM memory: NVIDIA Corporation MCP79 Memory Controller (rev b1) 00:03.2 SMBus: NVIDIA Corporation MCP79 SMBus (rev b1) 00:03.3 RAM memory: NVIDIA Corporation MCP79 Memory Controller (rev b1) 00:03.5 Co-processor: NVIDIA Corporation MCP79 Co-processor (rev b1) 00:04.0 USB controller: NVIDIA Corporation MCP79 OHCI USB 1.1 Controller (rev b1) 00:04.1 USB controller: NVIDIA Corporation MCP79 EHCI USB 2.0 Controller (rev b1) 00:06.0 USB controller: NVIDIA Corporation MCP79 OHCI USB 1.1 Controller (rev b1) 00:06.1 USB controller: NVIDIA Corporation MCP79 EHCI USB 2.0 Controller (rev b1) 00:08.0 Audio device: NVIDIA Corporation MCP79 High Definition Audio (rev b1) 00:09.0 PCI bridge: NVIDIA Corporation MCP79 PCI Bridge (rev b1) 00:0b.0 SATA controller: NVIDIA Corporation MCP79 AHCI Controller (rev b1) 00:0c.0 PCI bridge: NVIDIA Corporation MCP79 PCI Express Bridge (rev b1) 00:15.0 PCI bridge: NVIDIA Corporation MCP79 PCI Express Bridge (rev b1) 00:16.0 PCI bridge: NVIDIA Corporation MCP79 PCI Express Bridge (rev b1) 02:00.0 VGA compatible controller: NVIDIA Corporation GT218M [GeForce 310M] (rev a2) 02:00.1 Audio device: NVIDIA Corporation High Definition Audio Controller (rev a1) 03:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 03) 04:00.0 Network controller: Qualcomm Atheros AR9285 Wireless Network Adapter (PCI-Express) (rev 01)
查看USB接口:lsusb 1 2 3 4 5 6 7 $ lsusb Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 003: ID 04f2:b071 Chicony Electronics Co., Ltd 2.0M UVC Webcam / CNF7129 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 003 Device 002: ID 046d:c52b Logitech, Inc. Unifying Receiver Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
查看驱动:lsmod 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 $ lsmod Module Size Used by psmouse 91236 0 pci_stub 12550 1 vboxpci 22896 0 vboxnetadp 25636 0 vboxnetflt 27291 0 vboxdrv 300552 3 vboxnetadp,vboxnetflt,vboxpci bnep 18895 2 rfcomm 58045 0 bluetooth 391253 10 bnep,rfcomm 6lowpan_iphc 18262 1 bluetooth nvidia 9704627 55 uvcvideo 71465 0 videobuf2_vmalloc 13048 1 uvcvideo videobuf2_memops 13170 1 videobuf2_vmalloc videobuf2_core 48344 1 uvcvideo v4l2_common 15132 1 videobuf2_core videodev 131265 3 uvcvideo,v4l2_common,videobuf2_core media 20899 2 uvcvideo,videodev mxm_wmi 12893 0 snd_hda_codec_realtek 70241 1 snd_hda_codec_generic 62873 1 snd_hda_codec_realtek snd_hda_codec_hdmi 46396 4 arc4 12536 2 ath9k 122222 0 ath9k_common 24878 1 ath9k ath9k_hw 430710 2 ath9k_common,ath9k ath 24182 3 ath9k_common,ath9k,ath9k_hw mac80211 559049 1 ath9k coretemp 13201 0 kvm_intel 137114 0 snd_hda_intel 29285 5 kvm 388518 1 kvm_intel snd_hda_controller 29944 1 snd_hda_intel snd_hda_codec 120371 5 snd_hda_codec_realtek,snd_hda_codec_hdmi,snd_hda_codec_generic,snd_hda_intel,snd_hda_controller snd_hwdep 13272 1 snd_hda_codec snd_pcm 87194 4 snd_hda_codec_hdmi,snd_hda_codec,snd_hda_intel,snd_hda_controller snd_seq_midi 13324 0 joydev 17113 0 snd_seq_midi_event 14475 1 snd_seq_midi snd_rawmidi 25722 1 snd_seq_midi cfg80211 418839 4 ath,ath9k_common,ath9k,mac80211 snd_seq 56592 2 snd_seq_midi_event,snd_seq_midi snd_seq_device 14137 3 snd_seq,snd_rawmidi,snd_seq_midi serio_raw 13251 0 snd_timer 28648 2 snd_pcm,snd_seq snd 66670 21 snd_hda_codec_realtek,snd_hwdep,snd_timer,snd_hda_codec_hdmi,snd_pcm,snd_seq,snd_rawmidi,snd_hda_codec_generic,snd_hda_codec,snd_hda_intel,snd_seq_device shpchp 32143 0 soundcore 14599 2 snd,snd_hda_codec video 19475 0 i2c_nforce2 13050 0 wmi 18689 1 mxm_wmi asus_laptop 23914 0 sparse_keymap 13708 1 asus_laptop mac_hid 13059 0 input_polldev 14247 1 asus_laptop drm 255469 3 nvidia parport_pc 32021 0 ppdev 17391 0 lp 13299 0 parport 40836 3 lp,ppdev,parport_pc hid_logitech_dj 18077 0 usbhid 47035 0 hid 95946 3 usbhid,hid_logitech_dj r8169 61579 0 mii 13654 1 r8169 ahci 25622 2 libahci 26970 1 ahci
查看kernel版本:uname -r 1 2 3 4 5 6 7 8 $ uname -r 3.16.0-31-generic $ cat /proc/version Linux version 3.16.0-31-generic (buildd@lamiak) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #41~14.04.1-Ubuntu SMP Wed Feb 11 19:30:43 UTC 2015 $ uname -a Linux lin-K40IE 3.16.0-31-generic #41~14.04.1-Ubuntu SMP Wed Feb 11 19:30:43 UTC 2015 i686 i686 i686 GNU/Linux
查看ubuntu版本:sudo cat /etc/issue 1 2 $ cat /etc/issue Ubuntu 14.04.2 LTS \n \l
查看目录容量 du -h 如du /home -h
命令
解释
du -h
查看当前目录大小
du {folder} -h
查看{foulder}文件夹的大小
1 2 3 4 5 6 7 8 9 10 11 12 13 $ du -h 64K ./android 36K ./Image_Processing 24K ./Windows 24K ./French_Learning 68K ./Linux 52K ./computer_system 16K ./Programming 16K ./matlab 472K . $ du ./Linux/ -h 68K ./Linux/
查看硬盘详细信息 hdparm 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 $ sudo fdisk -l Disk /dev/sda: 320.1 GB, 320072933376 bytes 255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000bd319 Device Boot Start End Blocks Id System /dev/sda1 * 2048 616753151 308375552 83 Linux /dev/sda2 616755198 625141759 4193281 5 Extended /dev/sda5 616755200 625141759 4193280 82 Linux swap / Solaris $ sudo hdparm -tT /dev/sda1 /dev/sda1: Timing cached reads: 1970 MB in 2.00 seconds = 985.40 MB/sec
查看系统信息:dmesg 1 2 3 4 5 6 7 8 ##$ dmesg | more ## | more 是分小段显示 [ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Initializing cgroup subsys cpuacct [ 0.000000] Linux version 3.16.0-31-generic (buildd@lamiak) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #41~14.04.1-Ubuntu SMP Wed Feb 11 19:30:43 UTC 2015 (Ubuntu 3.16.0-31.41~14.04.1-generic 3.16.7-ckt5) [ 0.000000] KERNEL supported cpus: [ 0.000000] Intel GenuineIntel
查看本次开机运行时间:uptime 1 2 $ uptime 22:34:02 up 1:00, 3 users, load average: 0.63, 0.60, 0.52
查看登录信息 last 1 2 3 4 5 6 7 $ last | more lin pts/11 :0 Mon Mar 23 22:16 still logged in lin pts/0 :0 Mon Mar 23 22:15 still logged in lin :0 :0 Mon Mar 23 21:34 still logged in reboot system boot 3.16.0-31-generi Mon Mar 23 21:33 - 22:34 (01:01) lin pts/8 :0 Mon Mar 23 20:01 - down (00:00) lin pts/8 :0 Mon Mar 23 19:51 - 20:01 (00:10)
查看进程 1 2 3 4 5 6 7 8 9 10 11 $ ps aux | more USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 4600 3752 ? Ss 21:33 0:01 /sbin/init root 2 0.0 0.0 0 0 ? S 21:33 0:00 [kthreadd] root 3 0.1 0.0 0 0 ? S 21:33 0:07 [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S< 21:33 0:00 [kworker/0:0H] root 7 0.4 0.0 0 0 ? S 21:33 0:17 [rcu_sched] root 8 0.0 0.0 0 0 ? S 21:33 0:00 [rcu_bh] lin 1766 0.0 0.0 4240 268 ? Ss 21:34 0:00 //bin/dbus-daemon --fork --print-pid 5 --print-address 7 --session lin 1778 0.1 0.0 5048 3276 ? Ss 21:34 0:06 dbus-daemon --fork --session --address=unix:abstract=/tmp/dbus-C1wqkhxGdv lin 1789 0.0 0.0 4920 2076 ? Ss 21:34 0:00 upstart-event-bridge
root and lin 都是用户, pid代表进程ID号,可以通过kill PID 来强制结束某个进程。 也可以用top命令:1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ top top - 22:39:00 up 1:05, 3 users, load average: 0.82, 0.65, 0.55 Tasks: 199 total, 2 running, 197 sleeping, 0 stopped, 0 zombie %Cpu(s): 2.9 us, 0.7 sy, 0.0 ni, 96.0 id, 0.2 wa, 0.0 hi, 0.3 si, 0.0 st KiB Mem: 4148372 total, 1954148 used, 2194224 free, 49900 buffers KiB Swap: 4193276 total, 0 used, 4193276 free. 692576 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2105 lin 20 0 448600 136012 75324 S 2.3 3.3 2:27.26 compiz 1122 root 20 0 350200 146452 47920 S 2.0 3.5 1:12.27 Xorg 2839 lin 20 0 145148 33336 27404 S 2.0 0.8 0:28.74 gnome-terminal 13 root 20 0 0 0 0 S 1.3 0.0 0:49.85 ksoftirqd/1 2252 lin 20 0 454396 102052 41972 S 1.3 2.5 2:30.96 dropbox
查看历史命令 history 1 2 $ history #show history commands $ history -c #clear history commands in terminal (就是没办法通过向上键得到上一条命令)
如果想history命令也输出时间戳,执行下面的命令,再执行history1 export HISTTIMEFORMAT="%F %T "
可以在终端用Ctrl+R搜索历史命令,快速! 注:看到你想要的命令后按下左键或者右键,就可以在执行这条命令之前编辑它了]
默认情况下,命令历史被储存在.bash_history文件中,即使$ history -c ,历史也还在。
Linux 用户管理 创建用户 删除用户 用户权限管理 用户组 Linux 网络管理 ping命令 ifconfig 相关命令 配置网卡的IP地址 配置网卡的硬件地址 将网卡禁用 ifconfig eth0 down 将网卡启用 ifconfig eth0 up 不带参数的ifconfig 命令可以显示当前启动的网络接口 重启网卡 route 命令 使用ssh Linux 编程基础 环境变量env的查看与修改 环境变量是一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息。例如PATH,当要求系统运行一个程序而没有告诉它程序所在的完整路径时,系统除了在当前目录下面寻找此程序外,还应到PATH中指定的路径去找。用户通过设置环境变量,来更好的运行进程。 终端输入env查看所有的环境变量。
1 2 3 4 5 6 7 8 9 10 $ env ORBIT_SOCKETDIR=/tmp/orbit-bearthur SSH_AGENT_PID=1486 TERM=xterm SHELL=/bin/bash XDG_SESSION_COOKIE=c7856cbb97b9b99b583752ea00000007-1298359229.627829-1075292124 GST_ID3V2_TAG_ENCODING=GBK:UTF-8:GB18030 WINDOWID=81788933 GNOME_KEYRING_CONTROL ...(这里省略很多行)
有时候编译程序需要设置新的环境变量:export 环境变量=新值 // 比如: export DISPLAY =”localhost:0.0” 要加” “, 赋值时=两边不要加空格
需求例子: 我按装好了matlab,但是每次想启动matlab的时候都要跑到/usr/local/MATLAB/R2010b/bin目录下去执行” ./matlab”,我想直接就敲matlab启动。 解决方法: 把matlab的路径添加到修改环境变量PATH。1 2 3 4 5 6 7 8 $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games $ matlab matlab: command not found $ export PATH=$PATH:"/usr/local/MATLAB/R2010b/bin" $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/MATLAB/R2010b/bin $ matlab
重启电脑,发现再执行matlab还是command not found,查看PATH发现PATH已经恢复为原来的值了。这是因为上面的修改只是临时修改,如果想永久修改环境变量的值。需要修改~/.bashrc文件。1 2 $ vi .bashrc // 往最后一行添加export PATH=$PATH:"/usr/local/MATLAB/R2010b/bin" $ source .bashrc //使生效
参考:ubuntu 环境变量修改和恢复总结
nano编辑器 vi编辑器 编辑模式;命令模式;ex模式。 按esc键可以转到命令模式,按很多键可以转到编辑模式(比如i),按”:”可以进入ex模式, 前面带”:”的都是ex命令。
常见命令
命令
解释
vi hello.cpp
进入vi编辑器,编辑hello.cpp文件
光标的移动
上下左右,enter键。或是:左(h);右(l,空格键);上(k);下(j)
:w
保存
:q
退出
:wq
保存并退出
:q!
退出(不保存)
i
在当前光标前插入字符
a
在当前光标后插入字符
A
跳到行末尾进行编辑
o(小写字母o)
在当前行下面插入一行
O(大写字母O)
在当前行上面插入一行
x或nx(n代表数字)
从当前光标处往右删除(剪切)一个或n个字符
D或dd
剪切整行
Y或y
复制整行
nyy (n代表数字)
向下复制n行,同理,向下剪切n行为ndd
/hello 或?hello
搜索hello这个词,再按”n”向后查找,”N”向前查找
u,U(大写:回退到进入本行后的所有操作)
撤销命令
:se nu
查看行号(debug的时候经常用到,用来定位出错行)
:se nonu
隐藏行号
:%s/hello/bye/g
把所有的”hello”都变成”bye”
gcc编译器 编译 1 2 3 4 $ vi max.c $ gcc -c max.c $ ls max.o max.o
链接 1 2 3 $ gcc max.o -o run $ ll run -rwxrwxr-x 1 lin lin 7394 May 5 00:02 run*
静态链接库的编译链接 静态链接库在链接后,被引用的代码讲被复制到最终的可执行文件中,可脱离函数库直接运行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 bear@bear-K40IE:~/program/sec$ ls add.c add.o first1.c lib src bear@bear-K40IE:~/program/sec$ ar rv libtools.a add.o ar: creating libtools.a a – add.o bear@bear-K40IE:~/program/sec$ ls add.c add.o first1.c lib libtools.a src bear@bear-K40IE:~/program/sec$ vi sub.c bear@bear-K40IE:~/program/sec$ ls add.c add.o first1.c lib libtools.a src sub.c bear@bear-K40IE:~/program/sec$ gcc -c -O sub.c bear@bear-K40IE:~/program/sec$ ls add.c add.o first1.c lib libtools.a src sub.c sub.o bear@bear-K40IE:~/program/sec$ ar rv libtools.a sub.o a - sub.o bear@bear-K40IE:~/program/sec$ mv libtools.a ./lib bear@bear-K40IE:~/program/sec/lib$ vi my.h bear@bear-K40IE:~/program/sec/lib$ ls libtools.a my.h bear@bear-K40IE:~/program/sec$ vi first1.c bear@bear-K40IE:~/program/sec$ gcc -c -I./ first1.c bear@bear-K40IE:~/program/sec$ ls add.c add.o first1.c first1.o lib my.h src sub.c sub.o bear@bear-K40IE:~/program/sec$ gcc -c first1.c ear@bear-K40IE:~/program/sec$ gcc first1.o -L./lib -ltools -o first1 bear@bear-K40IE:~/program/sec$ ls add.c add.o first1 first1.c first1.o lib my.h src sub.c sub.o bear@bear-K40IE:~/program/sec$ ./first1 VALUE+10=110 VALUE-10=90 bear@bear-K40IE:~/program/sec$ gcc first1.o -o first1 first1.o: In function `main': first1.c:(.text+0x19): undefined reference to `add' first1.c:(.text+0x48): undefined reference to `sub' collect2: ld returned 1 exit status
这是add.c文件:1 2 3 4 5 #include <stdio.h> int add(int a,int b) { return a+b; }
这是sub.c文件:1 2 3 4 5 #include<stdio.h> int sub(int a,int b) { return a-b; }
这是first1.c文件:1 2 3 4 5 6 7 8 #include<stdio.h> #include"my.h" main () { fprintf(stderr,"VALUE+10=%d\n",add(VALUE ,10)); fprintf(stderr,"VALUE-10=%d\n",sub(VALUE ,10)); }
动态链接库的编译链接 动态链接库在内存中只包含一份可执行的映像,所有链接到动态链接库的程序都共享这份映像!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 bear@bear-K40IE:~/program/third$ vi first.c bear@bear-K40IE:~/program/third$ gcc -fPIC -c first.c bear@bear-K40IE:~/program/third$ ls first.c first.o bear@bear-K40IE:~/program/third$ file first.o first.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped bear@bear-K40IE:~/program/third$ gcc -shared -o libabc.so first.o bear@bear-K40IE:~/program/third$ ls first.c first.o libabc.so bear@bear-K40IE:~/program/third$ ls first.c first.o libabc.so bear@bear-K40IE:~/program/third$ vi hello.c bear@bear-K40IE:~/program/third$ ls first.c first.o libabc.so hello.c bear@bear-K40IE:~/program/third$ gcc -c hello.c -O bear@bear-K40IE:~/program/third$ ls first.c first.o libabc.so hello.c hello.o bear@bear-K40IE:~/program/third$ gcc hello.o -L./ -labc -s hello gcc: hello: No such file or directory bear@bear-K40IE:~/program/third$ echo LD_LIBRARY_PATH LD_LIBRARY_PATH bear@bear-K40IE:~/program/third$ export LD_LIBRATY_PATH=./ bear@bear-K40IE:~/program/third$ gcc hello.o -L./ -labc -s hello gcc: hello: No such file or directory bear@bear-K40IE:~/program/third$ gcc hello.o -labc -s hello #少了个-o gcc: hello: No such file or directory bear@bear-K40IE:~/program/third$ ls first.c first.o libabc.so hello.c hello.o bear@bear-K40IE:~/program/third$ gcc hello.o -labc -s -o hello /usr/bin/ld: cannot find -labc bear@bear-K40IE:~/program/third$ ls first.c first.o libabc.so hello.c hello.o bear@bear-K40IE:~/program/third$ echo LD_LIBRARY_PATH #少了个$ LD_LIBRARY_PATH bear@bear-K40IE:~/program/third$ export LD_LIBRATY_PATH="./" bear@bear-K40IE:~/program/third$ echo LD_LIBRARY_PATH LD_LIBRARY_PATH bear@bear-K40IE:~/program/third$ echo $LD_LIBRARY_PATH bear@bear-K40IE:~/program/third$ export LD_LIBRATY_PATH=./ #注意拼写啊! bear@bear-K40IE:~/program/third$ echo $LD_LIBRARY_PATH bear@bear-K40IE:~/program/third$ echo $LD_LIBRARY_PATH bear@bear-K40IE:~/program/third$ export LD_LIBRARY_PATH=./ bear@bear-K40IE:~/program/third$ echo $LD_LIBRARY_PATH ./ bear@bear-K40IE:~/program/third$ gcc hello.o -L./ labc -s -o hello gcc: labc: No such file or directory bear@bear-K40IE:~/program/third$ gcc hello.o -L./ -labc -s -o hello bear@bear-K40IE:~/program/third$ ls first.c first.o libabc.so hello hello.c hello.o bear@bear-K40IE:~/program/third$ ./hello 240 is the result
这是first.c文件:1 2 3 4 5 #include <stdio.h> int fx(int a,int b) { return a*b; }
这是hello.c文件:1 2 3 4 5 #include <stdio.h> main() { fprintf(stderr,"%d is the result \n",fx(10,24)); }
如何写makefile文件 demo_1: 有多个C++类与一个main函数,普通写法(直观) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 all: human.o point.o beast.o nature.o fight.o log.o random.o g++ random.o beast.o nature.o point.o fight.o human.o log.o -Wall -g -o run -lstdc++ -lm human.o: human.cc human.h point.h global.h g++ -c human.cc -lstdc++ -lm fight.o: fight.cc g++ -c fight.cc -lstdc++ -lm point.o: point.cc point.h g++ -c point.cc -lstdc++ -lm beast.o: beast.cc beast.h point.h g++ -c beast.cc -lstdc++ -lm nature.o: nature.cc nature.h g++ -c nature.cc -lstdc++ -lm log.o: log.h log.cc g++ -c log.cc -lstdc++ -lm random.o: random.h random.cc g++ -c random.cc -lstdc++ -lm clean: rm *.o
demo_2: 有多个依赖于OpenCV的C++类与一个main函数, 高级写法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 CC = g++ CFLAGS = -Wall -g INFLAGS = -I/usr/local/include/opencv -I/usr/include/opencv2 LFLAGS = -L/usr/local/lib/ SRC = Main.cpp ObjDet.cpp OBJ = $(SRC:.cpp=.o) TARGET = run DEPEND = -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching -std=c++11 .cpp.o: $(CC) -c $(INFLAGS) $(CFLAGS) $(SRC) $(TARGET): $(OBJ) $(CC) $(LFLAGS) $(OBJ) -o $(TARGET) $(DEPEND) clean: rm *.o
使用gdb调试程序 shell基础 Github的使用 使用Markdown写技术博客 Linux下MySQL的使用 常用的Linux快捷键 常见linux问题及解决办法